note 19518 added to language.oop

From: Date: Fri, 01 Mar 2002 15:52:35 +0000
Subject: note 19518 added to language.oop
Groups: php.notes 
Request: Send a blank email to php-notes+get-27232@lists.php.net to get a copy of this message
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"; -- http://www.php.net/manual/en/language.oop.php http://master.php.net/manage/user-notes.php?action=edit+19518 http://master.php.net/manage/user-notes.php?action=delete+19518 http://master.php.net/manage/user-notes.php?action=reject+19518

« previous php.notes (#27232) next »