开发者

How to bypass a low priority line of code havin fatal error?

开发者 https://www.devze.com 2023-04-12 17:50 出处:网络
Example : Say I have a logging class, and I use a global variable instance of it throughout the code. $logger = new Logger();

Example :

Say I have a logging class, and I use a global variable instance of it throughout the code.

$logger = new Logger();

function correctWorking()
{
   global $logger;
   $logger->log("this is correct");
}

function failing()
{
   $logger->log("this is fatal error");  /* here fatal error comes, :  Call to a member  
                                          function log() on a non-object in ...
                                         */
   moreI开发者_C百科mportantWork();
}

Please do not suggest better coding practices,I am working on them. My main curiousity is, how can I bypass the fatal error line, if the error occurs, as logging is not as important as keeping the app running.


You cannot.

You can suppress the error output like @$logger->log("this is fatal error"); but:

  • This is sloppy coding;
  • The script still terminates.

Instead, fix the fatal error.


You can not do this with fatal errors. On lower level errors you could write your own error_handler which creates an exception from them. Then you can use try { ... } catch { /* do nothing here */ } to avoid the termination of your script.


You need to put global $logger; in the failing() function, that appears to be your problem.


If i understand you correct you want to skip errors in your code and go on executing the script? You can use the @ operator to supress fatal errors

@$logger->log("this is fatal error");

for more examples http://de3.php.net/manual/en/language.operators.errorcontrol.php

0

精彩评论

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