-
-
Save Paladin/5273393 to your computer and use it in GitHub Desktop.
$fred = "Joe"; | |
$fred::myName(); // makes a static call to Joe::myname(); | |
class Sam | |
{ | |
public $fred = "Joe"; | |
function whoAreYou() | |
{ | |
$this->fred::myName(); // Erors out with unexpected :: error | |
} | |
} | |
class George | |
{ | |
public $fred = "Joe"; | |
function whoAreYou() | |
{ | |
$fred = $this->fred; | |
$fred::name(); // calls Joe::name() successfully | |
} | |
} | |
Why does George's syntax work but not Sam's? What do I do to Sam's to make it work? |
This will work in PHP 5.4, but not in any earlier versions.
Example 1 and 3 work in 5.4, #2 doesn't. And that's the approach I'm trying to use, without the extra assignment step in George. Is there no way to make it work?
I think it's how PHP associates the :: operator. It's trying to do $this->(fred::myName()) not ($this->fred)::myName()
Yes, but I think I tried ($this->fred)::name() as well. I know I tried it with the {} we use in double-quoted strings, anyway.
I think the trick of treating a variable name as a class name only works on variable names, not on class properties. I don't know any way of doing it without assigning your class property to a variable first. If you figure something out, I'd love to know your solution. I'll noodle on this more tonight too.
It looks so ugly, and seems so inconsistent, but then, this is PHP we're talking about.
BTW, putting $fred = $this->fred; in the class method will succeed, so it's the class attribute, not the class itself, that is the problem.