<?php
use ProxyManager\Factory\LazyLoadingValueHolderFactory;
// a class which does a lot of expensive thing
class Foo
{
public function __construct()
{
echo '<b>Foo constructor called</b>' . PHP_EOL;
}
public function getValue()
{
return 'Value of Foo' . PHP_EOL;
}
}
$factory = new LazyLoadingValueHolderFactory();
echo 'create lazy proxy of Foo class' . PHP_EOL;
$lazyproxy = $factory->createProxy(Foo::CLASS,
function (&$wrappedObject, $proxy, $method, $params, &$initializer) {
$wrappedObject = new Foo();
// don't initialize class again. Use same object
$initializer = null;
return true;
}
);
echo 'Foo proxy created' . PHP_EOL;
echo $lazyproxy->getValue();