A simple class for facilitating quicker use of PHP's ReflectionClass.

dev-main 2024-10-25 15:52 UTC

This package is auto-updated.

Last update: 2024-11-25 16:08:00 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

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.