PHP code example of stratadox / proxy

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

    

stratadox / proxy example snippets



use Stratadox\Proxy\ProxyLoader;

class MyLoader implements ProxyLoader
{
    private $repository;

    public function __construct(MyRepository $repository)
    {
        $this->repository = $repository;
    }

    public function loadTheInstance(array $data): object
    {
        return $this->repository->byId($data['id']);
    }
}


use Stratadox\Proxy\BasicProxyFactory;

$proxyFactory = BasicProxyFactory::for(MyProxy::class, new MyLoader());


/** @var \Stratadox\Proxy\ProxyFactory $proxyFactory */
$proxyFactory->create(['id' => 1]);


use Stratadox\Proxy\BasicProxyFactory;
use Stratadox\Proxy\CompositeProxyFactory;

// making the factory:
$proxyFactory = CompositeProxyFactory::decidingBy('type', [
    'car' => BasicProxyFactory::for(CarProxy::class, $loader),
    'painting' => BasicProxyFactory::for(PaintingProxy::class, $loader),
]);

// creating the proxies:
$carProxy = $proxyFactory->create(['type' => 'car', 'id' => 1]);
$paintingProxy = $proxyFactory->create(['type' => 'painting', 'id' => 5]);


class Foo {
    private $foo;
    public function __construct(string $foo) {
        $this->foo = $foo;
    }
    public function isEqual(Foo $other): bool {
        return $this->foo === $other->foo;
    }
}


class Foo {
    private $foo;
    public function __construct(string $foo) {
        $this->foo = $foo;
    }
    public function isEqual(Foo $other): bool {
        return $this == $other;
    }
}


class Foo {
    private $foo;
    public function __construct(string $foo) {
        $this->foo = $foo;
    }
    public function foo(): string {
        return $this->foo;
    }
    public function isEqual(Foo $other): bool {
        return $this->foo() === $other->foo();
    }
}