Skip to content

Instantly share code, notes, and snippets.

@Paladin
Last active December 15, 2015 14:19
Show Gist options
  • Save Paladin/5273393 to your computer and use it in GitHub Desktop.
Save Paladin/5273393 to your computer and use it in GitHub Desktop.
Not sure how "more code" helps, Joel, but...
$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?
@Paladin
Copy link
Author

Paladin commented Mar 29, 2013

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.

@joelclermont
Copy link

This will work in PHP 5.4, but not in any earlier versions.

@Paladin
Copy link
Author

Paladin commented Mar 29, 2013

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?

@joelclermont
Copy link

I think it's how PHP associates the :: operator. It's trying to do $this->(fred::myName()) not ($this->fred)::myName()

@Paladin
Copy link
Author

Paladin commented Mar 29, 2013

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.

@joelclermont
Copy link

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.

@Paladin
Copy link
Author

Paladin commented Mar 29, 2013

It looks so ugly, and seems so inconsistent, but then, this is PHP we're talking about.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment