当我尝试在新的本地机器上部署 Symfony 2.8 项目时,我在多个代码位置遇到了这个错误:
"Warning: get_class() expects parameter 1 to be object, null given"
在stackoverflow上没有找到这种情况,花了一些时间找出原因。
最佳答案
正如关于此问题的其他地方所述,在 PHP 7.2 get_class
中手动状态:
Note: Explicitly passing NULL as the object is no longer allowed as of PHP 7.2.0. The parameter is still optional and calling get_class() without a parameter from inside a class will work, but passing NULL now emits an E_WARNING notice.
正如您在 your own answer 中发现的那样.
但是您说:
So downgrading php version to 7.1 solved the problem.
降级 PHP 通常不是解决问题的最佳或长期方法*;相反,您需要将 get_class
包装在 checker 函数中,例如 is_object
,或者相反,is_null
:
$baz = new class();
$className = false; // catch all if $baz is not an object
if(is_object($baz)){
$className = get_class($baz);
}
- 如 Nicco Hasse 所述
如果这个问题发生在 Symfony 代码中,仅仅改变 Symfony 代码并不是一个好主意。由于升级 Symfony 不是一种选择,正如 OP 的回答中所述,保持 7.1 似乎是最好的解决方案
我会说,虽然“修复”Symphony 代码可能很麻烦,但我建议将限定符 is_object
添加到 Symphony 代码中,然后在它发布时更新到最新的 Symphony 版本 (我希望能解决这个问题)。
关于php - get_class() 期望参数 1 为对象,给定为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49751991/