Skip to content

Instantly share code, notes, and snippets.

@quickshiftin
Last active December 8, 2015 18:38
Show Gist options
  • Save quickshiftin/01da6e4f22e34c01b9a6 to your computer and use it in GitHub Desktop.
Save quickshiftin/01da6e4f22e34c01b9a6 to your computer and use it in GitHub Desktop.
Syntactic sugar to quickly create a closure for an instance method. Especially useful for creating 'callables' to private methods.
<?php
class NNReflectionMethod extends ReflectionMethod
{
static public function closure($object, $method)
{
if(!is_object($object)) {
throw new InvalidArgumentException('$object must be an object');
}
$oRm = new ReflectionMethod($object, $method);
return $oRm->getClosure($object);
}
}
@quickshiftin
Copy link
Author

Read the blog article here for the full writeup!

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