开发者

Ignore methods by the php interpretor

开发者 https://www.devze.com 2023-01-27 20:53 出处:网络
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

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.

0

精彩评论

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