It is possible in php to ignore some methods by the php interpretor? I need to ignore some methods or functions if the project is, for example, in the release mode, and executed them i开发者_运维问答f the project is in the debug mode.
If you're indeed talking about methods (not functions) then the solution is using Overloading:
class MyClass
{
static public $debugging = true;
public function __call($function, $arguments)
{
if (!self::$debugging)
trigger_error("Cannot call $function in release mode!", E_USER_ERROR);
return call_user_func_array(array($this,'__real_'.$function), $arguments);
}
protected function __real_debug($a,$b,$c)
{
// Do something here
}
}
Then for all not implicitly declared methods of MyClass
, the overloaded __call
method will be invoked. If you do:
$c = new MyClass();
$c->debug(1,2,3);
Then, if $debugging
is true, the protected __real_debug
is called.
BTW: the above sample is not restricted to PHP 5.3. It works for any PHP 5.x version.
精彩评论