PHP code example of cwola / reflector

1. Go to this page and download the library: Download cwola/reflector library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cwola / reflector example snippets




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