Re: strange behavior on manipulating the array of class object

From: Date: Tue, 26 Jun 2001 12:26:20 +0000
Subject: Re: strange behavior on manipulating the array of class object
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-55260@lists.php.net to get a copy of this message
Hello, I've not tried your code yet, but after examining it a short while I think your problem is that PHP's copying behaviour. It means that assignments (including argument/parameter) *copy* values to the target variable no matter it is an object or a string or whatever. You should use references , perhaps it helps. The '&' is your very best friend! IMHO, current() and next() also returns just the copy of the array elements as well as "foreach" construct, beware of it. visit this one http://www.aditus.nu/jpgraph/jpg_phpoo.php there are some short examples of PHP's copying behaviour. the script below shows the expected result (loops rewritten in one() and two()) ------------------------------------------ <?php class foo { var $cls; function foo() { $this->cls = array(); } function add( & $cls ) { // $this->cls[] = & $cls; array_push($this->cls, $cls); } function one() { for ($i = 0; $i < count($this->cls) ; $i++ ) { print '<B>foo->one()</B> invokes <B>'.get_class( $this->cls[$i] )."->one()</B><BR>\n"; $this->cls[$i]->one(); } } function two() { for ($i = 0; $i < count($this->cls) ; $i++ ) { print '<B>foo->two()</B> invokes <B>'.get_class( $this->cls[$i] )."->two()</B><BR>\n"; $this->cls[$i]->two(); } } } 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 ); print_r($foo); $foo->one(); $foo->two(); ?> ----- Original Message ----- From: "Moriyoshi Koizumi" <readjust@deneb.freemail.ne.jp> To: <php-general@lists.php.net> Sent: 2001. június 25. 15:29 Subject: [PHP] strange behavior on manipulating the array of class object > 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() ) > -------------------------------------------------------------------- > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net >

« previous php.general (#55260) next »