strange behavior on manipulating the array of class object

From: Date: Mon, 25 Jun 2001 13:29:28 +0000
Subject: strange behavior on manipulating the array of class object
Groups: php.general 
Request: Send a blank email to php-general+get-55169@lists.php.net to get a copy of this message
Hello. I'm new to this list, and I once looked over the archives, but the problem like this might not have been proposed, so I ask... please see the code below sorry to be too long. (the result of this code is supposed to be seen with HTML browser) -------------------------------------------------------------------- <?php class foo { var $cls; function foo() { $this->cls = array(); } function add( $cls ) { array_push ( $this->cls, $cls ); } function one() { reset( $this->cls ); while( $obj = current( $this->cls ) ) { print '<B>foo->one()</B> invokes <B>'.get_class( $obj )."->one()</B><BR>\n"; $obj->one(); next( $this->cls ); } } function two() { reset( $this->cls ); while( $obj = current( $this->cls ) ) { print '<B>foo->two()</B> invokes <B>'.get_class( $obj )."->two()</B><BR>\n"; $obj->two(); next( $this->cls ); } } } class bar { var $some; function bar() { $this->some = '<FONT color="red">set in constructor</FONT> ( this value was expected to be changed at foo->one() ) '; } function one() { $this->some = 'modified'; } function two() { print '<B>bar->two->some</B> = '.$this->some."<BR>"; } } $fubar = new foo(); $fubar->add( new bar() ); $fubar->add( new bar() ); $foo = new foo(); $foo->add( $fubar ); $foo->one(); $foo->two(); ?> -------------------------------------------------------------------- and the results should have been like -------------------------------------------------------------------- foo->one() invokes foo->one() foo->one() invokes bar->one() foo->one() invokes bar->one() foo->two() invokes foo->two() foo->two() invokes bar->two() bar->two->some = modified foo->two() invokes bar->two() bar->two->some = modified -------------------------------------------------------------------- but you probably obtain -------------------------------------------------------------------- foo->one() invokes foo->one() foo->one() invokes bar->one() foo->one() invokes bar->one() foo->two() invokes foo->two() foo->two() invokes bar->two() bar->two->some = set in constructor ( this value was expected to be changed at foo->one() ) foo->two() invokes bar->two() bar->two->some = set in constructor ( this value was expected to be changed at foo->one() ) --------------------------------------------------------------------

« previous php.general (#55169) next »