Get "PHP 8 in a Nuthshell" (Soon includes PHP 8.4)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

Accessing private methods and properties outside of class using reflection in PHP

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.

Learn the fundamentals of PHP 8 (including 8.1, 8.2, and 8.3), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

Like this article?

Buy me a coffee

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?