Created
May 24, 2011 04:23
-
-
Save Gasol/988136 to your computer and use it in GitHub Desktop.
Using Reflection to get private property value in PHP
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 A { | |
private $data; | |
public function setData($value) | |
{ | |
$this->data = $value; | |
} | |
} | |
$a = new A; | |
$a->setData('hello world'); | |
#echo $a->data; //error | |
$reflector = new ReflectionClass('A'); | |
$properties = $reflector->getProperties(); | |
foreach ($properties as $property) { | |
$property->setAccessible(true); | |
echo $property->getValue($a) . PHP_EOL; // hello world | |
#$property->setAccessible(false); | |
#echo $property->getValue($a) . PHP_EOL; // error | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment