Feature #43819

Support lazy dependency injection of properties

Added by Christopher Hlubek almost 9 years ago. Updated over 8 years ago.

Status:
Resolved
Priority:
Should have
Category:
-
Target version:
-
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

Related to TYPO3 Core - Feature #48960: Extbase: Backport lazy dependency injectionRejectedMarc Bastian Heinrichs2013-06-08

Actions

Also available in: Atom PDF