PHP code example of c01l / phpdecorator-memo

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

    

c01l / phpdecorator-memo example snippets


class TestClass
{
    #[Memo(["bar"])]
    public function foo(int $bar, int $x): int
    {
        return $x;
    }
}

$obj = new TestClass();
$obj = (new \C01l\PhpDecorator\DecoratorManager())->decorate($obj);

$obj->foo(1,2) // 2
$obj->foo(2,3) // 3
$obj->foo(1,3) // 2, because foo is not really executed again, but the cached version is used

#[Memo(["bar", "x"])]
public function foo($bar, $x) { return $x; }

#[Memo]
public function foo($bar, $x) { return $x; }

...

$obj->foo(1,2); // 2
$obj->foo(1,3); // 2
$obj->foo(2,5); // 2
$obj->foo(4,6); // 2

class A {
    public int $x, $y, $z;
    public function getX() { return $x; }
}

...

#[Memo(["bar->z", "bar->getX()"])]
public function foo(A $bar, $x) { return $x; }