Created
January 6, 2014 22:10
-
-
Save superic/8290704 to your computer and use it in GitHub Desktop.
Call PHP anonymous function that is an object property. More information: http://eric.tumblr.com/post/72481137519/call-php-closure-that-is-an-object-property
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$person = (object) array( | |
"name" => "Eric", | |
"location" => "SF", | |
"sayHello" => function () { | |
return "Hello!"; | |
} | |
); | |
// does not work with anonymous functions in PHP | |
var_dump($person->sayHello()); | |
// this works but is ugly | |
$sayHello = $person->sayHello; | |
var_dump($sayHello()); | |
// this works as a one-liner but is still ugly | |
var_dump(call_user_func($person->sayHello)); | |
// this works as a one-liner too... still ugly | |
var_dump($person->sayHello->__invoke()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment