Php reflection get private property

In PHP, the visibility of a property, a method, or a constant can be defined by prefixing the declaration using keywords public, protected or private. Here is how these modifiers work.

  • public - Class members declared public can be accessed everywhere.
  • protected - Class members declared protected can be accessed only within the class itself and by inheriting and parent classes.
  • private - Class members declared as private may only be accessed by the class that defines the member.

Generally, the class members are declared private for certain reasons. One is to implement encapsulation which makes the class members available inside of the originating class only i.e. to hide data from the user of the class and can only be modified using public getter and setter methods.

But in certain scenarios, you might want to access these private members outside of the class. There’s a workaround in PHP using which you can do so. First, let’s check how to access private methods.

Accessing private methods

First, check the following class.

class Foo 
{
    private function privateMethod[] {
        return 'Hogwarts';
    }
}

As you can see, the privateMethod is a private method and if we want to access it outside of the class like so, we would get a fatal error.

$foo = new Foo;
$foo->privateMethod[]; 
// Fatal error:  Uncaught Error: Call to private method 
// Foo::privateMethod[] from context

To get around this, we can use PHP’s in-built ReflectionMethod class which can give handlful of information about the class. And also can “reverse engineer” things for us.

Basically, it shows the “mirror” to the class so that it can learn about itself. And here’s how you can use it.

$reflectionMethod = new ReflectionMethod['Foo', 'privateMethod'];
$reflectionMethod->setAccessible[true];

echo $reflectionMethod->invoke[new Foo]; // Hogwarts

As you can see, the ReflectionMethod constructor accepts two parameters: “Class name” and “Method name”. In our case, we passed in Foo as the class name and privateMethod method name as we want to access this method.

Next, we’ll need to make the private method accessible outside of the class. For this, we have used the setAccessible method on the object and set it to true. This will allow protected and private methods to be invoked on-the-fly.

And lastly, we can invoke the method using the invoke method on the object and passing in the object of the class [new Foo] as its only parameter for which we’re accessing the method.

And that is how you can access a private method of the class.

Accessing private properties

Similarly, you can also access the private properties of the class but the only distinction here is instead of using ReflectionMethod, we’d need to used ReflectionProperty class like so.

class Foo 
{
    private $privateProperty = 'Harry Potter!';    
}

$property = new ReflectionProperty['Foo', 'privateProperty'];
$property->setAccessible[true];

echo $property->getValue[new Foo]; // Harry Potter!

As you can see, in this case, we’ve used getValue to fetch the value of the property.

Beep! Beep! I'm also running a YouTube channel which I hope you're going to love!

A couple of weeks ago I was working on a very tricky issue on ProxyManager.

The problem is simple: instantiating ReflectionClass or ReflectionProperty is slow, and by slow, I mean really slow!

The reason for this reasearch is that I'm trying to optimize a "hydrator" to work with larger data-sets by still keeping a low initialization overhead.

PHP 5.4 to the rescue!

PHP 5.4 comes with a new API for Closures, which is Closure#bind[].

Closure#bind[] basically allows you to get an instance of a closure with the scope of a given object or class. Neat! That's basically like adding APIs to existing objects!

Let's break some OOP encapsulation to fit our needs.

The techniques to access private members are already explained on the PHP manual, but I am going to make a simplified example anyway.

Here's what you have to do to steal Kitchen#yummy from following object:

Bài mới nhất

Chủ Đề