PHP code example of timandes / reflection

1. Go to this page and download the library: Download timandes/reflection 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/ */

    

timandes / reflection example snippets


interface Foo
{
    public function foo();
}

interface Bar
{
    public function bar();
}

class DefaultFooBar implements Foo, Bar
{
    public function foo()
    {
        echo 'DefaultFooBar::foo()' . PHP_EOL;
        return 'foo';
    }
    public function bar()
    {
        echo 'DefaultFooBar::bar()' . PHP_EOL;
        return 'bar';
    }
}

use Timandes\Reflection\Proxy;

$fooBar = new DefaultFooBar();
$fooBarProxy = Proxy::newProxyInstance($fooBar, [Foo::class, Bar::class], function($proxy, \ReflectionMethod $method, array $args) use($fooBar) {
    echo "Before invoking" . PHP_EOL;
    return $method->invokeArgs($fooBar, $args);
});
$fooBarProxy->foo();

class BaseFoo
{
    public function bar()
    {
        echo 'BaseFoo::bar()' . PHP_EOL;
        return 'bar';
    }
}

use Timandes\Reflection\Enhancer;

$foo = new BaseFoo();
$fooProxy = Enhancer::createInstance(BaseFoo::class, function($object, \ReflectionMethod $method, array $args) use($foo) {
    echo 'Before invoking' . PHP_EOL;
    return $method->invokeArgs($foo, $args);
});
$fooProxy->bar();