Skip to content

Repository files navigation

Build StatusLatest Stable VersionLatest Unstable VersionLicense

A PHP reflection library to directly access protected/private properties and call protected/private methods. Static properties and methods can also be accessed/invoked from a class directly.

This library works with major versions of PHP from 5.3 to 8.5.

Installation

For PHP 8.0+, please use version 2.0:

composer require crowdstar/reflection:~2.0.0

For old versions of PHP (PHP 5.3 to PHP 7.4), please use version 1.0 instead:

composer require crowdstar/reflection:~1.0.0

How To Use It

Three static methods are provided to access protected/private properties and call protected/private methods of an object/class:

  • CrowdStar\Reflection\Reflection::getProperty(): Get current value of a property.
  • CrowdStar\Reflection\Reflection::setProperty(): Set a new value to a property.
  • CrowdStar\Reflection\Reflection::callMethod(): Call a method of a class/object.

Here is a sample code showing how to use them:

<?phpdeclare(strict_types=1);
require__DIR__ . '/vendor/autoload.php';
useCrowdStar\Reflection\Reflection;
class Helper
{
privatestring$key;
privatestaticstring$keyStatic;
privatefunctionget(): string
{
return'Private method invoked.';
}
privatestaticfunctiongetStatic(int$i, int$j): string
{
return"Private static method invoked with parameter {$i} and {$j}.";
}
}
$helper = newHelper();
// Access properties and invoke methods from objects.
Reflection::setProperty($helper, 'key', 'Set value to a private property.');
echo"Output 1: ", Reflection::getProperty($helper, 'key'), PHP_EOL;
echo"Output 2: ", Reflection::callMethod($helper, 'get'), PHP_EOL, PHP_EOL;
// Access static properties and invoke static methods from objects.
Reflection::setProperty($helper, 'keyStatic', 'Set value to a private static property.');
echo"Output 3: ", Reflection::getProperty($helper, 'keyStatic'), PHP_EOL;
echo"Output 4: ", Reflection::callMethod($helper, 'getStatic', [1, 2]), PHP_EOL, PHP_EOL;
// Static properties and methods can also be accessed/invoked from a class directly.
Reflection::setProperty(Helper::class, 'keyStatic', 'Set another value to a private static property.');
echo"Output 5: ", Reflection::getProperty(Helper::class, 'keyStatic'), PHP_EOL;
echo"Output 6: ", Reflection::callMethod(Helper::class, 'getStatic', [3, 4]), PHP_EOL;
?>

About

A PHP reflection library to directly access protected/private properties and call protected/private methods.

Resources

Stars

1 star

Watchers

4 watching

Forks

Releases

Used by

Contributors

Languages