Skip to content

Instantly share code, notes, and snippets.

@superic
Created January 6, 2014 22:10
Show Gist options
  • Save superic/8290704 to your computer and use it in GitHub Desktop.
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
$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