dpmwy80068 2013-01-15 16:05
浏览 54

如何禁用部分代码的错误/异常处理程序?

I'm using PHP version 5.3.19 and I have the following code :

set_exception_handler(array($this->main_model,'nothing'));
set_error_handler(array($this->main_model,'nothing'));
error_reporting(0);
foreach ($data as $row){
...
}
restore_error_handler();

Now i just tried hacking with the nothing there, it's a function doing nothing because i tried setting them as null but that didn't changed, and this doesn't change it too.

I've also tried wrapping in try catch but it stills handles the error.

I can't disable the errors completly as im using them with a custom error handler, what i want is to disable the foreach loop for errors, i've also called the function with @ but since i used custom error handler it no longer worked.

I've read here that when using custom handler php ignores error_reporting.

How can i get the foreach to not handle errors no matter what?

  • 写回答

1条回答 默认 最新

  • doushiposong30622 2013-01-15 17:19
    关注

    Neither set_error_handler() nor set_exception_handler() enable us to log fatal errors such E_ERROR, E_PARSE and E_CORE_ERROR.

    The solution to the problem is register_shutdown_function()

    This function lets you register a callback function that will be run when PHP is shutting down, even when it is forced to shut down due to entering an unstable state.

    Here is a Custom Class which enables you to Register or Unregister a ShutDown Function.

    class UnregisterableCallback{
    
        // Store the Callback for Later
        private $callback;
    
        // Check if the argument is callable, if so store it
        public function __construct($callback)
        {
            if(is_callable($callback))
            {
                $this->callback = $callback;
            }
            else
            {
                throw new InvalidArgumentException("Not a Callback");
            }
        }
    
        // Check if the argument has been unregistered, if not call it
        public function call()
        {
            if($this->callback == false)
                return false;
    
            $callback = $this->callback;
            $callback(); // weird PHP bug
        }
    
        // Unregister the callback
        public function unregister()
        {
            $this->callback = false;
        }
    }
    

    You can use this Class logic along with your code.

    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号