references-weirdness???

From: Date: Wed, 28 Nov 2001 23:25:57 +0000
Subject: references-weirdness???
Groups: php.dev 
Request: Send a blank email to php-dev+get-71969@lists.php.net to get a copy of this message
why do the following two examples result in different things, even though i would expect the same behaviour? in words: i pass a reference of a class-property to a variable $walk, if i overwrite $walk, the class-property has a new value too (seems logical, since we are working with references not pointers) BUT if i use a class-method which returns a reference to this class-property, to set $walk ... and then i overwrite $walk the class-property didnt change code-example <?php class test { var $data; function test() { $this->data[0] = 'level zero'; $this->data[1] = 'level one'; } function &getData( $id ) { return $this->data[$id]; } } $test = new test; print('<br>'); print_r($test); // the following block overwrites $test->data[0] with $test->data[1] $walk = &$test->data[0]; $walk = $test->data[1]; print('<br>'); print_r($test); $test = new test; // but this one does NOT !!! $walk = $test->getData(0); $walk = $test->data[1]; print('<br>'); print_r($test); ?> -- Wolfram

« previous php.dev (#71969) next »