首页 > 解决方案 > PHP 7 - 如何捕捉“不能使用类型的对象......作为数组”?

问题描述

出于某种奇怪的原因,当它发生时我无法捕捉到它

这是代码,它应该可以工作,但我不断得到

Fatal error: Uncaught Error: Cannot use object of type Stuff as array in...

代码:

try{
    class Stuff
    {

    }

    $stuff= new Stuff();
    $stuff["test"]=0;   <<<<<<< this should trigger the below catch 
}
catch (Exception $e) {
    $myLogger->Log($e);
}

谢谢

标签: php-7

解决方案


这是有效的答案:

try{
    class Stuff
    {
        $test = null; 
    }

    $stuff= new Stuff();
    $stuff->test = 0;
}
catch (Throwable $e) {
    $myLogger->Log($e);
}

推荐阅读