2

If objects are passed by reference in PHP5, why does the following always display 123 then xyz, as opposed to abc then xyz which is what I would expect?

<?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();

?>

Do I always have to explicitly store a reference like I have done with $elem in order to set properties on the element?

Gerald Versluis
  • 24,892
  • 5
  • 60
  • 84
Pez
  • 173
  • 1
  • 13

0 Answers0