Created
January 13, 2012 09:30
-
-
Save afoeder/1605272 to your computer and use it in GitHub Desktop.
Revisions
-
afoeder revised this gist
Jan 13, 2012 . 1 changed file with 8 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,12 @@ <?php /** * Please be aware that private properties are private for good purpose, * changing it during runtime so is only OK during Unit Testing for example. * So use this only if you're knowing what you're doing :) */ class BareDemoClass() { protected $interestingThing = 42; -
afoeder revised this gist
Jan 13, 2012 . 1 changed file with 18 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,14 +1,30 @@ <?php class BareDemoClass() { protected $interestingThing = 42; protected function niceButPrivateMethod() { return "DNA N47°16' E11°23'"; } } // during usual use, you'd have both the bare class and $instanciatedClass available $instanciatedClass = new BareDemoClass(); $reflectedProperty = new \ReflectionProperty('BareDemoClass', 'interestingThing'); $reflectedProperty->setAccessible(TRUE); // 42 echo $reflectedProperty->getValue($instanciatedClass); $reflectedMethod = new \ReflectionMethod('BareDemoClass', 'niceButPrivateMethod'); $reflectedMethod->setAccessible(TRUE); // DNA N47°16' E11°23' echo $reflectedMethod->invoke($instanciatedClass); // you could also get the reflected property or method using the object itself: $reflectedObject = new \ReflectionObject($instanciatedClass); $reflectedProperty = $reflectedObject->getProperty('interestingThing'); // continue like above; or $reflectedMethod = $reflectedObject->getMethod('niceButPrivateMethod'); ?> -
afoeder created this gist
Jan 13, 2012 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,14 @@ <?php class BareDemoClass() { protected $interestingThing = 42; } // during usual use, you'd have both the bare class and $instanciatedClass available $instanciatedClass = new BareDemoClass(); $reflectedProperty = new \ReflectionProperty('BareDemoClass', 'interestingThing'); $reflectedProperty->setAccessible(true); echo $reflectedProperty->getValue($instanciatedClass); // the idea is to have the property at first as "anonymous" property description without context; and with ->getValue you give the context: you pass in the desired object. ?>