Notice: Undefined property - how do I avoid that message in PHP?

五迷三道 提交于 2020-02-01 23:38:27

问题


Hello I am making this call:

$parts = $structure->parts;

Now $structure only has parts under special circumstances, so the call returns me null. Thats fine with me, I have a if($parts) {...} later in my code. Unfortunately after the code finished running, I get this message:

Notice: Undefined property: stdClass::$parts in ...

How can I suppress this message?

Thanks!


回答1:


The function isset should do exactly what you need.

PHP: isset - Manual

Example:

$parts = (isset($structure->parts) ? $structure->parts : false);



回答2:


maybe this

$parts = isset($structure->parts) ? $structure->parts : false ;



回答3:


With the help of property_exists() you can easily remove "Undefined property" notice from your php file.

Following is the example:

if(property_exists($structure,'parts')){ $parts = $structure->parts; }

To know more http://php.net/manual/en/function.property-exists.php



来源:https://stackoverflow.com/questions/10143172/notice-undefined-property-how-do-i-avoid-that-message-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!