开发者

How to silence warnings

I have a class that inherits from a another class.

I am overloading one method; PHP screams about this.

How do I silence this message? It clutters my debug logs.

Declaration of Dashboard_Abstract::factory() should be compatible with that of Abstract::factory()

class Abstract{
  static public function factory($param){
  ...
  ...
  }
}


class Dashboard_Abstract extends Abstract{
  static public function factory($param1,$param2){
  ...
  ...
  }
}

Look, fellow developers. There is a difference between errors and warnings: means,

"Look, if you are new to this you might be doing something wrong here, If you are experienced, you might be doi开发者_如何学Pythonng right, so we will let you, the developer, decide."

So, why won't you let me decide? Is it so bad to use all of a language abilities to the max, even if some think it is an error (although, obviously, it is not).


You can use the error_reporting() function to override it at runtime, and there's an identically named parameter in php.ini you can set to make the change permanent. However, warnings are there for a reason and generally you should fix your code instead of just silencing them.


Well, since it is a specialized class, altering the method signature should not trigger a warning. It doesn't in pure OOP languages. And E_STRICT is really a special kind of purposed warning message (trying to impose a non-semantic coding standard).

But anyway, there is an easy workaround for your case. You can make the method signature compatible by just making parameters optional in the overriding method:

class Abc {
  static public function factory($param) { }
}  

class Xyz extends Abc {
  static public function factory($param1, $param2=NULL) { }
}

Notice the $param2=NULL in the overloading method signature. With this trick the more specialized class/object can still be used where a parent object was expected.
You might use an assert($param2!==NULL) within the method instead if it is required.

(The E_STRICT notice for eventually undefined parameters in your static factory methods -which are only ever used with explicit classnames- really makes no sense here. But won't get fixed http://bugs.php.net/bug.php?id=41461 in the php.net implementation.)


You're not overriding it correctly because the number of variables has changed. It will continue to yell at you about this, as it should. Keep the number of arguments the same, use default arguments if you have to.


You should fix the problem. If you have a child class overriding a method in a parent class, the signature of the method must be compatible. That means that it must have the same or greater visibility, the same staticity (?!) and the same number or fewer arguments.

Either Abstract should take two parameters, or Dashboard_Abstract should take one. I can't tell you which way round it should be without seeing what these classes actually do...


You shouldn't silence that message, instead you should make sure that the overriding method is compatible with the original one. If you need the overriding method to accept a different number of parameters, you could rewrite the original method to use a dynamic list of arguments - see http://php.net/manual/en/function.func-get-args.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜