crowdstar / reflection
Allow to directly access protected/private properties and call protected/private methods.
Installs: 10 520
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 6
Forks: 0
Open Issues: 0
Requires
- php: >=5.3.3
Requires (Dev)
- phpunit/phpunit: ~4.0|~5.0
- squizlabs/php_codesniffer: >=2.0
This package is auto-updated.
Last update: 2022-06-28 21:19:55 UTC
README
A PHP reflection library to directly access protected/private properties and call protected/private methods.
This library works with major versions of PHP from 5.3 to 7.4.
Installation
composer require crowdstar/reflection:~1.0.0
Sample Usage
<?php require __DIR__ . '/vendor/autoload.php'; use CrowdStar\Reflection\Reflection; class Helper { private $key; private static $keyStatic; private function get() { return 'private method invoked'; } private static function getStatic(int $i, int $j) { return "private static method invoked with parameter {$i} and {$j}"; } }; $helper = new Helper(); // Access properties and invoke methods from objects. Reflection::setProperty($helper, 'key', 'value from a private property'); echo "Output 1: ", Reflection::getProperty($helper, 'key'), "\n"; echo "Output 2: ", Reflection::callMethod($helper, 'get'), "\n"; // Access static properties and invoke static methods from objects. Reflection::setProperty($helper, 'keyStatic', 'value from a private static property'); echo "Output 3: ", Reflection::getProperty($helper, 'keyStatic'), "\n"; echo "Output 4: ", Reflection::callMethod($helper, 'getStatic', array(1, 2)), "\n"; // Static properties and methods can also be accessed/invoked from a class directly. Reflection::setProperty(Helper::class, 'keyStatic', 'another value from a private static property'); echo "Output 5: ", Reflection::getProperty(Helper::class, 'keyStatic'), "\n"; echo "Output 6: ", Reflection::callMethod(Helper::class, 'getStatic', array(3, 4)), "\n"; ?>