If we have to access private method for out side the class then we can get it easily using below simple example:
Class Stack
{
private function myPrivateMethod()
{
return 'I am out from private method.';
}
}
$stack = new Stack();
$reflection_class = new ReflectionClass($stack);
$private_method = $reflection_class->getMethod('myPrivateMethod');
$private_method->setAccessible(true);
echo $private_method->invoke($stack);
Output :
I am out from private method.
n the above example we have made object of ReflectionClass and set the true in setAccessible() method. ReflectionClass is reporting information about a class
No comments:
Post a Comment