PHP:致命错误调用成员函数......在非对象上
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11825592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP: Fatal Error Call to a member function ... on a non-object
提问by Flater
I'm having an issue with PHP as it keeps throwing the Exception mention in the title. It fails on the following line:
我遇到了 PHP 问题,因为它不断在标题中抛出异常提及。它在以下行失败:
$item->getDescription();
I understand what the error should mean ($itemis null). However, $item is not null.
我明白错误应该意味着什么($item为空)。但是,$item 不为空。
The scenario is as follows: This is a script that syncs products from a supplier to a store. For that purpose, I have created my own class (SimpleProduct). This class has a getDescription() function.
场景如下: 这是一个将产品从供应商同步到商店的脚本。为此,我创建了自己的类 (SimpleProduct)。这个类有一个 getDescription() 函数。
The problem is that the data I'm receiving tend to have a lot of garbage, like items that haven't been filled in yet. The script should skip these items and keep on iterating across the rest of the products. This fatal error kills the entire script.
问题是我收到的数据往往有很多垃圾,比如尚未填写的项目。脚本应跳过这些项目并继续迭代其余产品。这个致命错误会杀死整个脚本。
I've already tried implementind safeguards to prevent this from happening, but it still occurs constantly. Here's the current code (some snippets removed as they arent pertinent to the currect case).
我已经尝试过实施保护措施来防止这种情况发生,但它仍然不断发生。这是当前代码(删除了一些片段,因为它们与当前案例无关)。
//This is part of a class that performs the sync
public function syncProduct($item) {
if(empty($item)) { return "Not a product"; }
else { var_dump($item) }
$foo = $item->getDescription();
}
When checking the var_dump result, I get an object with some values filled in. Seeing as it is of the correct type (SimpleProduct) and it is not empty/null, I would suspect this error to stop occurring, but it still does.
检查 var_dump 结果时,我得到一个填充了一些值的对象。看到它是正确的类型 (SimpleProduct) 并且它不是空/空,我怀疑这个错误会停止发生,但它仍然会发生。
Also note that several product syncs have already occurred without any errors before this one pops up, so I know the code is valid. Somehow, this specific case slips past my null-checks.
另请注意,在此弹出之前,已经发生了多个产品同步而没有任何错误,因此我知道代码是有效的。不知何故,这个特定案例跳过了我的空检查。
Is my null-check faulty? How can an error for a non-object be thrown when the object in question does exist?
我的空检查有问题吗?当有问题的对象确实存在时,如何抛出非对象的错误?
回答by Kemal Fadillah
Instead of checking whether if the variable is empty, why not check whether if it's an instance of SimpleProduct?
与其检查变量是否为空,不如检查它是否是 的实例SimpleProduct?
if ($item instanceof SimpleProduct)
{
}
回答by Mihai Stancu
Your null check is not preventing the object to be used even if it is nullcontains non-objects.
即使null包含非对象,您的 null 检查也不会阻止要使用的对象。
Use this:
用这个:
public function syncProduct($item) {
var_dump($item);
if($item InstanceOf SimpleProduct) {
$foo = $item->getDescription();
}
return "Not a product";
}
I stand corrected! I didn't notice the return statement. The other case for this to occur would be if the value from $itemwould be non-empty but not a product either - most likely a scalar or array because using objects as other types of objects issue a different error regarding methods not being found.
我站纠正!我没有注意到返回语句。发生这种情况的另一种情况是,如果 from 的值$item不是空的,但也不是产品 - 最有可能是标量或数组,因为将对象用作其他类型的对象会发出与未找到方法有关的不同错误。
回答by Big Tree
Surely the object is still not available in the context of the function syncProduct.
当然,该对象在函数syncProduct 的上下文中仍然不可用。
Try to do a var_dump($item) to confirm its there and execute it within the else part of the code to ensure its not empty.
尝试执行 var_dump($item) 以确认其存在并在代码的 else 部分执行它以确保其不为空。
回答by Tom
To check if $item is an object You can use is_object()
检查 $item 是否为对象您可以使用is_object()
回答by andromeda
I also run into a similar problem where after running this:
我也遇到了类似的问题,在运行这个之后:
$user = DB::getInstance()->action($action="SELECT * ", 'users');
Then checking whether $user is an instance of DB, I found that it wasn't. I then decided separate it as follows:
然后检查 $user 是否是 DB 的实例,我发现它不是。然后我决定将其分开如下:
$user = DB::getInstance();
$user->action($action="SELECT * ", 'users');
After doing this and using instanceof() method it shows that it is now an instance and the fatal call to member function error disappears.
执行此操作并使用 instanceof() 方法后,它显示它现在是一个实例,并且对成员函数错误的致命调用消失了。

