cwola / reflector
Providing simple reflections (Cwola library).
v1.0.1
2022-07-10 04:38 UTC
Requires
- php: >=8.0.0
README
PHP Reflections(Cwola library).
Overview
Providing simple reflections for PHP.
Requirement
- PHP8.0+
Installation
composer require cwola/reflector
Usage
<?php
use Cwola\Reflector\ReflectionClass;
class Foo {
/**
* @var string
*/
private string $privateString = 'Private!!';
/**
* @param string $text
*
* @return string
*/
private function privateMethod(string $text): string {
return 'Private: ' . $text;
}
}
$foo = new Foo;
$reflector = ReflectionClass::make($foo);
// Property
$reflectionPrivateProperty = $reflector->property('privateString')->accessible(true);
echo $reflectionPrivateProperty->get(); // Private!!
$reflectionPrivateProperty->set('Modified');
echo $reflectionPrivateProperty->get(); // Modified
// Method
$reflectionPrivateMethod = $reflector->method('privateMethod')->accessible(true);
echo $reflectionPrivateMethod->call('hijack'); // Private: hijack
// call reflection method
$reflectionPrivateMethod->isConstructor(); // false