#23351 [Opn->Bgs]: Coding Question

From: Date: Fri, 25 Apr 2003 20:24:23 +0000
Subject: #23351 [Opn->Bgs]: Coding Question
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-38412@lists.php.net to get a copy of this message
ID: 23351 User updated by: dave at socrates dot thinkhost dot com Reported By: dave at socrates dot thinkhost dot com -Status: Open +Status: Bogus Bug Type: *General Issues Operating System: FreeBSD4.7 PHP Version: 4.3.2RC1 New Comment: Bogus, error in my code $this->{$name} works as expected Previous Comments: ------------------------------------------------------------------------ [2003-04-25 15:05:57] dave at socrates dot thinkhost dot com This is a coding question, because I believe that I should be able to do this, but I can not find reference to it anywhere in the documentation (on or off the PHP site). The problem is best expressed through an example, note that PHP doesn't complain, it just doesn't do what I want it to. Consider the code fragment: class MyClass { var $day; var $month; var $year; // ...constructor... function getFieldByName ($name) { return $this->$name; } function updateFieldByName ($name, $value) { $this->$name = $value; } } Now consider the following sequence of commands: $r = new MyClass(); $r->updateFieldByName("year", "2003") echo $r->getFieldByName("year"); Which outputs nothing, because not only did it fail to retrieve the local variable, but it failed to update the local variable. Member functions can be called like so: $this->$function_name_in_this_variable(); Why can't we access local variables the same way? I have also tried: $this->{$name} Based on the fact that ${$name} is the same as $$name. In the class that I am dealing with, there are so many local variables that an update function for each field is impractical at best. Is there a way to accomplish what I am trying to do, or am I merely using incorrect syntax? ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=23351&edit=1

« previous php.bugs (#38412) next »