Skip to main content

how to access a private member outside the class in php (php Hack)

If you want to access a private variable/method outside the class... here is a trick to do the same:

class A
{
private $notVisibleOutside = 1;
}


$a = new ReflectionClass("A");
$property = $class->getProperty("notVisibleOutside ");
$property->setAccessible(true);

$a1 = new A();
echo $property->getValue($a1); // Work

So, the basic idea is to
- create a reflection class in php
- make the property or method accessible
- use that property to access the private variables/methods

Comments