Temporarily Disable Yii Error Handler To Catch Php Warning

Is it possible to temporarily disable the Yii error handler to allow a PHP warning through? Or (after seeing what I’m trying to do)… am I going about this all wrong and is there a better way to approach this?

I guess this could apply to ANY situation when you want to capture a PHP warning, but for me, I’m trying to rename a folder. The function doesn’t throw an exception but does return a true/false based on the result. During testing, I forced it to error, but the application isn’t handling it how I expected. I want to log the error and continue processing, but the Yii error handler is kicking in and not letting me check the result of the PHP function rename().

Config:




'errorHandler'=>array(

    'errorAction'=>'site/error',

),



Code:




if ( rename('/path/to/current/folder', '/path/with/new/name') ) { // with Yii's error handler enabled, the code stops here

    echo 'Success. The folder was renamed';

}

else {

    echo 'Boo. The folder was NOT renamed';

}



Outside of Yii (in a plain 'ole PHP file), this works perfectly. Inside Yii, not so much.

If I disable the Yii error handler, it works ok. But I don’t want to disable it for my whole application just to suit this one situation.


defined('YII_ENABLE_ERROR_HANDLER') or define('YII_ENABLE_ERROR_HANDLER',false);

Can I disable the event handler before performing the function and then re-enable again after?


Yii::app()->attachEventHandler('onError',null);

After all that… the question is, what’s the best way to capture/handle PHP warnings and continue with the current script without disabling Yii’s error handler for the entire application?

Thanks in advance!

You’re looking for the @ operator. You add it before the method name when calling it to supress any errors caused by it. Be warned, it’s evil.




if ( @rename('/path/to/current/folder', '/path/with/new/name') ) {

}



You described the only use case for this operator. Other than that it should be avoided, because if the called function causes a fatal error it is extremely hard to locate it.

No, my friend, I have been looking for you! A thousand thank you’s!

I did come across this operator during my many frustrated hours of Googling, but wasn’t familiar with it so wasn’t sure it was the right choice. Plus, I’ve been laying the blame solely on Yii’s over eager error handler that I couldn’t get to back down.

This answers my problems, despite its obvious "evilness".

"the least evil of the evil alternatives." … love it!

It still urge you to try and avoid it. In your rename example, you can make all required checks before actually calling rename to make sure it doesn’t trigger an error.