Created
October 28, 2016 07:13
-
-
Save am-/303f86168e363194929f5c957d34c525 to your computer and use it in GitHub Desktop.
This file contains 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
<?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