PHP DOM,类属性和通过引用传递对象

时间:2016-09-14 08:27:12

标签: php oop pass-by-reference

如果在PHP5中通过引用传递对象,为什么以下内容始终显示123然后xyz,而不是abc然后xyz这是我期望的?

<?php

class CustomDOMElement extends DOMElement
{
    public $custom_property = '123';

    public function echoCustomProperty()
    {
        var_dump($this->custom_property);
    }
}

$document = new DOMDocument();
$document->registerNodeClass('DOMElement', 'CustomDOMElement');
$document->loadHTML('<div>Hi, this is a test</div>');

$document->documentElement->custom_property = 'abc';
$document->documentElement->echoCustomProperty();

$elem = &$document->documentElement;
$elem->custom_property = 'xyz';
$elem->echoCustomProperty();

?>

我是否总是必须像$elem那样明确地存储引用,以便在元素上设置属性?

0 个答案:

没有答案