I just found this behavior when trying to implement a linked list with php.
In linked list it's common to say: 
$a =& $a->next; 
when traversing the list. but this doesn't work (it gives segmentation fault when 
trying to access the $head->next object (after the above assignment). 
but this work: 

$tmp =& $a;
$a =& $tmp->next; 

I don't know if this can be considered a bug or if it's designed to work that way. But 
I think it would be nice to be able to do it without creating temporary variable.  

here is the full code. I'm using php 4.2.2 and I'm running the code from comand line. 
(if I execute it from the web it gives something like 'document contains no data' 
instead of segmentation fault)


class test {
  var $name, $next;
  function test($n = '') {
    $this->name = $n;
    $this->next = null;
  function toString() {
    return $this->name;

$a = new test('a');
$a->next = new test('b');
$a->next->next = new test('c');

echo 'a '. $a->toString() ."\n";
echo 'a next '. $a->next->toString() ."\n";
echo 'a next next '. $a->next->next->toString() ."\n";

$tmp =& $a;  $a =& $tmp->next; // this is fine

//$a =& $a->next; // this will result in Segmentation fault

echo 'a '. $a->toString() ."\n";

echo 'a next '. $a->next->toString() ."\n";


I would be glad to hear other people's opinion on this.

- reynard 

The NEW Netscape 7.0 browser is now available. Upgrade now! 

Get your own FREE, personal Netscape Mail account today at http://webmail.netscape.com/

PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to