Re: Variable functions within an object
| From: | Julio Sergio Santana | Date: | Fri, 30 Jul 2004 15:57:45 +0000 |
| Subject: | Re: Variable functions within an object | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-192324@lists.php.net to get a copy of this message | ||
Curt Zirzow wrote:
$this->{$this->fname}(); or (what it actually is doing.. ) $func = $this->fname; $this->$func(); CurtThe point here is that the named function is outside the object. That is, $this->foo() doesn't exist, so $this->{$this->fname}(), does not work either. But if you look at your suggested construct, I wonder why $this->{$this->fname}() is sintactically correct while {$this->fname}() is not (since we just striped out the prefix '$this->' which means that the function is inside the object). Look for instance at a similar case when dealing with variable variables in arrays, this is what the online documentation says in this case: "In order to use variable variables with arrays, you have to resolve an ambiguity problem. That is, if you write $$a[1] then the parser needs to know if you meant to use $a[1] as a variable, or if you wanted $$a as the variable and then the [1] index from that variable. The syntax for resolving this ambiguity is: ${$a[1]} for the first case and ${$a}[1] for the second." So, I think the case is similar here Clearly, $this->fname() means "call the object's method 'fname'", but {$this->fname}(), would mean "call the function whose name is '$this->fname'". I think, for the sake of orthogonallity, this feature should be added to the language. Thank you. Sergio.