【发布时间】:2014-09-24 13:20:06
【问题描述】:
从 PHP5.4 开始,当您尝试使用隐式转换作为对象时,PHP 会抛出警告。
消息:从空值创建默认对象
通常,这可以通过显式声明变量类型来防止 - 例如
$thing = new stdClass();
但是当您开始处理将对象转换为 XML 的库时,这会变得非常烦人。所以你之前的代码说
$xml->authentication->identity->accountID->username = "myName";
变得臃肿
$xml = new stdClass();
$xml->authentication = new StdClass();
$xml->authentication->identity = new stdClass();
$xml->authentication->identity->accountID = new stdClass();
$xml->authentication->identity->accountID->username = new stdClass();
$xml->authentication->identity->accountID->username = "myName";
但在像使用 XML 这样的实例中,像这样的深节点树非常常见。
有没有一种替代方法可以以这种方式显式声明每个节点的每个级别,而不通过禁用警告来伪造它?
【问题讨论】: