Skip to content

Instantly share code, notes, and snippets.

@Gasol
Created May 24, 2011 04:23
Show Gist options
  • Save Gasol/988136 to your computer and use it in GitHub Desktop.
Save Gasol/988136 to your computer and use it in GitHub Desktop.
Using Reflection to get private property value in PHP
<?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