christopheraseidl / reflect
A simple class for facilitating quicker use of PHP's ReflectionClass.
Fund package maintenance!
christopheraseidl
Requires
- php: ^8.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.64
- pestphp/pest: ^3.5
- phpstan/extension-installer: ^1.4
- phpstan/phpstan: ^1.12
- phpstan/phpstan-deprecation-rules: ^1.2
- phpstan/phpstan-phpunit: ^1.4
This package is auto-updated.
Last update: 2024-11-25 16:08:00 UTC
README
Reflect is a simple wrapper around PHP's ReflectionClass that provides easy access to private properties and methods, supporting both instanced and static members with inheritance.
PHP Versions
- PHP 8.0+
Installation
You can install Reflect via composer:
composer require christopheraseidl/reflect
Usage
Accessing instance members
class MyClass { private string $private = 'private property'; private function method(): string { return 'private method'; } } $reflect = Reflect::on(new MyClass()); echo $reflect->private; // 'private property' echo $reflect->method(); // 'private method'
Accessing static members
class MyStaticClass { private static string $static = 'static property'; private static function staticMethod(): string { return 'static method'; } } $reflect = Reflect::on(MyStaticClass::class); echo $reflect->static; // 'static property' echo $reflect->staticMethod(); // 'static method'
Inheritance support
class ParentClass { private string $parentProp = 'parent property'; } class Child extends ParentClass { private string $childProp = 'child property'; } $reflect = Reflect::on(new Child()); echo $reflect->parentProp; // 'parent property' echo $reflect->childProp; // 'child property'
Non-instantiable classes
abstract class AbstractClass { private static string $env = 'development'; } $reflect = Reflect::on(AbstractClass::class); echo $reflect->env; // 'development'
Testing
composer test
Credits
License
The MIT License (MIT). Please see License File for more information.