Skip to content
Start » PHP » Executing protected and private methods in PHP

Executing protected and private methods in PHP

Private and protected methods should not be accessed outside the permitted scope of the class or subclass. However, there are some situations where this is necessary, for me in some contexts of automated tests (unit or integration), or in others where I am implementing a functionality and I want to perform specific executions to verify behaviors.

Regardless of why, and the good practice direction in which I would state, don't call private or protected methods outside of their scope, several languages support this possibility.

In PHP and Java, for example, we can use reflections (routines that allow exploring classes structurally).

Example of accessing protected methods in PHP

Let's check how to execute a protected method with private or protected visibility in PHP language (which can be used in frameworks like Laravel and Symphony).

Our example class is:

class Employee { private $salary = 1000; private function calcBonus() { return $this->salary * 0.1; } }

One class Employee with a private method capable of calculating the employee's bonus amount.

Using Reflection with PHP

With Reflection we will create a reflection class from the class Employee to extract the private method calc Bonus and run it in sequence:

$reflectionClass = new ReflectionClass(Employee::class); $reflectionMethod = $reflectionClass->getMethod('calcBonus'); $reflectionMethod->setAccessible(true); $reflectionMethod->invoke(new Employee()); // '100'

This strategy will work with methods private or protected.

We can refactor this code and access the desired method with reflection without needing to instantiate a reflection of the complete class:

$reflectionMethod = new ReflectionMethod(Employee::class, 'calcBonus'); $reflectionMethod->setAccessible(true); $reflectionMethod->invoke(new Employee()); // '100'

If you need to pass arguments to the method, instead of invoke use ReflectionMethod::invokeArgs.

Let's refactor our class so that the method calc Bonus get the calculation factor:

class Employee { private $salary = 1000; private function calcBonus($factor) { return $this->salary * $factor; } }

Then we can execute the method defining the arguments passing them as an array, in this case even passing a single argument we must send it inside the array:

$args = [0.1]; $reflectionMethod->invokeArgs(new Employee(), $args);

Helper to execute protected methods

Finally, add this function with a helper to your toolbox, making it easier to run protected and private methods in PHP in the future:

/** * Calls object method with arguments. * * @param object $object * @param string $method * @param array $args * @return mixed */ function callProtectedMethod(object $object, string $method, array $args = []) { $reflectionMethod = new ReflectionMethod(get_class($object), $method) ; $reflectionMethod->setAccessible(true); return $reflectionMethod->invokeArgs($object, $args); }

Run the helper as in the following example:

callProtectedMethod(new Employee(), 'calcBonus'); // without arguments //or callProtectedMethod(new Employee(), 'calcBonus', [0.2]); // with arguments

Leave a Reply

Your email address will not be published.

en_USEnglish
%d bloggers like this: