PHP DOM、类属性和通过引用传递对象
如果对象在PHP5中是通过引用传递的,为什么下面的内容总是显示PHP DOM、类属性和通过引用传递对象,php,oop,pass-by-reference,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); } }
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
那样显式存储引用