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
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
Post a Comment