Skip to content

Instantly share code, notes, and snippets.

@am-
Created October 28, 2016 07:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save am-/303f86168e363194929f5c957d34c525 to your computer and use it in GitHub Desktop.
Save am-/303f86168e363194929f5c957d34c525 to your computer and use it in GitHub Desktop.
<?php
class AnswerAdder
{
private function addTheAnswer($x)
{
return 42 + $x;
}
}
$callPrivateMethod = function ($object, $method, ...$args) {
$call = function ($method, ...$args) {
return $this->$method(...$args);
};
$newCall = $call->bindTo($object, get_class($object));
return $newCall($method, ...$args);
};
echo $callPrivateMethod(new AnswerAdder(), 'addTheAnswer', 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment