If we have to access private method for out side the class then we can get it easily using below simple example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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 :
1 | 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