Skip to content

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

License

Notifications You must be signed in to change notification settings

christopheraseidl/reflect

Repository files navigation

An easy way to utilize the power of PHP's ReflectionClass.

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.

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages