Feature #43819
Support lazy dependency injection of properties
Start date:
2012-12-09
Due date:
% Done:
100%
Estimated time:
PHP Version:
Has patch:
No
Complexity:
Description
<?php
namespace Test;
class SomeDependency {
public function __construct() {
echo "Construct SomeDependency\n";
}
public function foo() {
echo "Foo called\n";
}
}
class LazyProxy_Original {
//
// /**
// * @Flow\Inject(lazy=true)
// * @var \Test\SomeDependency
// */
// protected $someProperty;
public function doSomething() {
for ($i = 0; $i < 4; $i++) {
$this->someDependency->foo();
}
// $this->fooBar->unknown();
}
}
class LazyProxy extends LazyProxy_Original {
public function __construct() {
echo "Construct LazyProxy\n";
}
function __get($name) {
echo "LazyProxy get($name)\n";
switch ($name) {
case 'someDependency':
$object = new SomeDependency();
break;
default:
trigger_error(
'Undefined property via __get(): ' . $name,
E_USER_NOTICE);
return NULL;
}
$this->{$name} = $object;
return $object;
}
}
$lazy = new LazyProxy();
$lazy->doSomething();
?>
Related issues