#23351 [NEW]: Coding Question
| From: | dave at socrates dot thinkhost dot com | Date: | Fri, 25 Apr 2003 20:05:57 +0000 |
| Subject: | #23351 [NEW]: Coding Question | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-38411@lists.php.net to get a copy of this message | ||
From: dave at socrates dot thinkhost dot com
Operating system: FreeBSD4.7
PHP version: 4.3.2RC1
PHP Bug Type: *General Issues
Bug description: Coding Question
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 bug report at http://bugs.php.net/?id=23351&edit=1
--
Try a CVS snapshot: http://bugs.php.net/fix.php?id=23351&r=trysnapshot
Fixed in CVS: http://bugs.php.net/fix.php?id=23351&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=23351&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=23351&r=needtrace
Try newer version: http://bugs.php.net/fix.php?id=23351&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=23351&r=support
Expected behavior: http://bugs.php.net/fix.php?id=23351&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=23351&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=23351&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=23351&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=23351&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=23351&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=23351&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=23351&r=gnused