1

In php I can do this:

$class = 'Notes';
echo $class::message();

but it seems that from within a method, I can't do this:

echo ($this->myClass)::message(); 

and also cannot do this:

echo someFunctionThatReturnsClassName()::message();

Can anybody explain why? And give some rules about when and how this indirection mechanism works?

Thank you Gidi

shealtiel
  • 8,020
  • 18
  • 50
  • 82

1 Answers1

3

This is not an issue with variable variables, it is an issue with PHP not allowing this syntax. There is a proposal for fixing this in the core, however this one has not been accepted yet: http://wiki.php.net/rfc/fcallfcall (and also http://wiki.php.net/rfc/functionarraydereferencing)

maartenba
  • 3,344
  • 18
  • 31
  • Thank you, this answers very well my second example, bu not my first – shealtiel Jan 06 '11 at 14:44
  • Well yes, as your first one in theory is the same: by adding brackets (), you tell the PHP interpreter to execute that part of code and then call a static method on that one. Which will fails. If you do $temp = ($this->myClass); $temp::message(), it will work like a charm. – maartenba Jan 06 '11 at 14:48
  • Not sure about your point with adding brackets. $this->myClass::message() will not work as well. I know that using a $temp will do the job, my question is why, how $this->var is different from simpy $var. – shealtiel Jan 06 '11 at 14:54
  • Without the brackets, you have a special situation. PHP treats $this->var as getting a string value. If you say $this->var::foo(), you are calling foo() on a string and not on a "variable variable". – maartenba Jan 06 '11 at 14:57
  • interesting, so will this: $class='Notes'; ($class)::message(); fail as well? What about this: ('Notes')::message(); and this: (Notes)::message()? – shealtiel Jan 06 '11 at 15:05
  • ($class)::message(); may work, as I *think* PHP optimizes it to the version without brackets. The 2nd one will probably not work as well as the third one. It's been a while since my PHP certification exam :-) – maartenba Jan 06 '11 at 15:08