note 19518 deleted from language.oop by aidan
| From: | aidan@php.net | Date: | Sat, 03 Jul 2004 14:56:46 +0000 |
| Subject: | note 19518 deleted from language.oop by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-72463@lists.php.net to get a copy of this message | ||
Note Submitter: philhassey.maybe.at@hotmail.com
----
When trying to use a dynamically named array in a class, be careful of how you access your dynamic
array.
Incorrect:
$dynamic_name="myarray";
$this->$dynamic_name[0]="apples";
// Meant $this->m="apples";
$this->$dynamic_name[1]="oranges";
// Meant $this->y="oranges";
Correct way:
$dynamic_name="myarray";
$this->{$dynamic_name}[0]="apples";
// Meant $this->myarray[0]="apples";
$this->{$dynamic_name}[1]="oranges";
// Meant $this->myarray[1]="oranges";
The incorrect example written differently shows how you could interact with multiple dynamic
variable names.
$dynamic_names=array("name_one","name_two");
$this->$dynamic_names[0]="apples";
// Meant $this->name_one="apples";
$this->$dynamic_names[1]="oranges";
// Meant $this->name_two="oranges";
However this example might be more clearly written as
$dynamic_names=array("name_one","name_two");
$this->{$dynamic_names[0]}="apples";
// Meant $this->name_one="apples";
$this->{$dynamic_names[1]}="oranges";
// Meant $this->name_two="oranges";