Re: note 11676 added to Classes and Objects

From: Date: Thu, 01 Mar 2001 23:33:27 +0000
Subject: Re: note 11676 added to Classes and Objects
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-11758@lists.php.net to get a copy of this message
It is understandable that your original loop will not work, you are getting a *copy* of the object, not the original object itself. A loop such: for ($i=0; $i<count($header); $i++) $header[$i]->set_i('new string'); Will access the object directly, instead of the copy you get w/ foreach. Will add the comment above to your note. --- rich.ayotte@discoverniagara.com wrote: > Array of Objects > > First here is a simple class: > class text { > var $i; > function text($i = 'default') { > $this->i = $i; > } > > function set_i( $i ) { > $this->i = $i; > } > } > > Now create objects and put them in an array > $header[] = new text('foo'); > $header[] = new text('bar'); > > The following will NOT work: > > foreach($header as $i): > $i->set_i('new string'); > endforeach; > > You must do the following instead: > $j = 0; > foreach($header as $i): > $k = &$header[$j++]; > $k->set_i('new string'); > endforeach; > > It's quirky. Does anyone know of a better way to do > this? > > http://www.php.net/manual/en/language.oop.php > > > -- > PHP Notes Mailing List (http://www.php.net/) > To unsubscribe, e-mail: > php-notes-unsubscribe@lists.php.net > For additional commands, e-mail: > php-notes-help@lists.php.net > To contact the list administrators, e-mail: > php-list-admin@lists.php.net > ===== --- Jesus M. Castagnetto <jmcastagnetto@php.net> __________________________________________________ Do You Yahoo!? Get email at your own domain with Yahoo! Mail. http://personal.mail.yahoo.com/

« previous php.notes (#11758) next »