2010-11-30 64 views
0

有可能在php中忽略php解釋器的某些方法?如果項目處於發佈模式,則需要忽略某些方法或函數,並在項目處於調試模式時執行它們。忽略php解釋器的方法

回答

0

如果你確實談論的方法(而不是函數),然後將溶液使用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 
    } 
} 

,那麼對於所有不隱式聲明的MyClass方法,重載__call方法將被調用。如果你這樣做:

$c = new MyClass(); 
$c->debug(1,2,3); 

然後,如果$debugging是真實的,受保護的__real_debug被調用。

順便說一句:上面的示例不限於PHP 5.3。它適用於任何PHP 5.x版本。

+0

我也想過這個解決方案,但我試圖避免使用if語句每次我想做一些調試的東西。 – Madalina 2010-11-30 17:12:26