出于某种奇怪的原因,我只是无法在异常发生时捕获该异常
这是代码,它应该可以工作,但我不断得到
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);
}
谢谢
这是工作答案:
try{
class Stuff
{
$test = null;
}
$stuff= new Stuff();
$stuff->test = 0;
}
catch (Throwable $e) {
$myLogger->Log($e);
}
试试这个。 使用另一个捕获来获得致命错误
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()
}
试试这个。它总是有帮助
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);
}