出于某些奇怪的原因,我只是无法在发生异常时捕获该异常
这是代码,它应该可以工作,但我一直在获取
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);
}
谢谢
答案 0 :(得分:1)
这是工作答案:
try{
class Stuff
{
$test = null;
}
$stuff= new Stuff();
$stuff->test = 0;
}
catch (Throwable $e) {
$myLogger->Log($e);
}
答案 1 :(得分:0)
尝试一下。使用另一个捕获来获取致命错误
try{
class Stuff
{
}
$stuff= new Stuff();
$stuff["test"]=0; <<<<<<< this should trigger the below catch
} catch (Exception $e) {
$myLogger->Log($e);
} catch (Error $e) {
// Handle error
echo $e->getMessage(); // Call to a member function method() on string
} catch (Throwable $e) {
// Handle error
echo $e->getMessage(); // Call to undefined function undefinedFunctionCall()
}
答案 2 :(得分:-1)
尝试一下。总是有帮助的
try{
class Stuff
{
$test = null;
public function getTest()
{
return $this->test;
}
public function setTest($value)
{
$this->test = $value;
}
}
$stuff= new Stuff();
$stuff->setTest(0);
echo $stuff->getTest();
}
catch (Exception $e) {
$myLogger->Log($e);
}
否则,如果要将对象转换为数组,请尝试此操作。
$stuff= new Stuff();
$stuff = (array)$stuff;
这会将对象转换为数组(通过类型转换)
否则可以直接设置
try{
class Stuff
{
$test = null;
}
$stuff= new Stuff();
$stuff->test = 0;
}
catch (Exception $e) {
$myLogger->Log($e);
}