properties - 通知: Undefined property - how do I avoid that message in PHP?

标签 properties null php-5.3

您好,我正在打这个电话:

$parts = $structure->parts;

现在,$ structure在特殊情况下仅包含部分内容,因此该调用将返回null。没关系,我稍后在代码中有一个if($ parts){...}。不幸的是,代码运行完后,我收到以下消息:

注意:未定义的属性:stdClass::$ parts ...

如何隐藏此消息?

谢谢!

最佳答案

函数isset应该完全满足您的需求。

PHP: isset - Manual

例子:

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

关于properties - 通知: Undefined property - how do I avoid that message in PHP?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10143172/

相关文章:

android - 错误: attempt to index global object a nil value: corona SDK

objective-c - Objective-C 中 nil、NIL 和 null 之间的区别

php - 如何访问已转换为对象的数组的属性/值?

php - Ubuntu 12.04 PHP 5.5 和 apache2 2.4

ios - 我应该在 iOS 中制作我的 ivars 属性吗?

c# - 在C#中使用Lambda确定非相等属性值

c# - 事件处理程序中的属性值不正确

c# - 通过字符串名称设置/获取类属性

c# - 在重写 Equals 中进行空检查之前转换为对象

mysql - 无法使用 drupal 7 中的提要模块导入超过 200 条记录