1、Revisiting lazy objects in PHP and Symfony nicolasgrekasLazy LoadingDont do anything unless really neededLazy LoadingCan save time and memoryPerfect for short-lived requestsAllows creating circular object graphs(Provides resetting for free)The 4 kinds of Lazy Loading Lazy Initialization Value holder
2、s Virtual proxies Ghost objectsLazy InitializationCheck properties for a marker value(usually null)and load them on demandclass LazyInitializedClass public function getData()return$this-data?=$this-doGetData();Lazy InitializationThe implementation is laziness-awareValue HoldersAn object with a publi
3、c getValue()methodclass LocatorHolder public function _construct(#AutowireLocator(workflow,name)private ContainerInterface$workflows)public function getWorkflow(string$name)return$this-workflows-get($name);class IterableHolder public function _construct(#AutowireIterator(workflow)private iterable$wo
4、rkflows)public function getWorkflows():Generator foreach($this-workflows as$workflow)yield$workflow;Value HoldersThe consumers are laziness-awareVirtual ProxiesAn object with the same interface as the real objectThe real object is created just-in-timeclass EntityManager implements EntityManagerInter
5、face/.public function find(string$class,$id)/.class VirtualChildEntityManager extends EntityManager private parent$em;private bool$isInitialized=false;public function _construct(private Closure$initializer)public function find(string$class,$id)if(!$this-isInitialized)($this-initializer)($this);retur
6、n$this-em-find($class,$id);class VirtualProxyEntityManager implements EntityManagerInterface private EntityManagerInterface$em;private bool$isInitialized=false;public function _construct(private Closure$initializer)public function find(string$class,$id)if(!$this-isInitialized)($this-initializer)($th