note 11676 added to Classes and Objects
| From: | rich dot ayotte at discoverniagara dot com | Date: | Thu, 01 Mar 2001 23:07:33 +0000 |
| Subject: | note 11676 added to Classes and Objects | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-11748@lists.php.net to get a copy of this message | ||
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