cwola/reflector

Providing simple reflections (Cwola library).

v1.0.1 2022-07-10 04:38 UTC

This package is auto-updated.

Last update: 2024-06-12 04:45:51 UTC


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

Licence

MIT