Php implements code for delayed loading through virtual proxy. This product was learned from Martin's "Enterprise Application Architecture Model", which assists PHP with the dynamic language features and can implement LazyLoad much easier than Java ). Basically, this product was learned from Martin's Enterprise Application Architecture Model, which assists PHP with the dynamic language features and can implement LazyLoad much easier than Java ). The basic principle is to use a Virtual Proxy as a placeholder. once a member (method or attribute) of the Proxy object is accessed, the loading is triggered.
However, the version I implemented has limitations:
It is applicable only to objects and cannot represent arrays or other basic data types (it must be encapsulated with built-in objects such as ArrayObject)
After being proxies, some interface implementations with operator overload properties will become invalid, such as the ArrayAccess indexer and Itreator iterator. if this proxy is used to handle delayed loading of collection types, you also need to inherit a subclass for special processing so that the foreach iteration can be used externally.
Demo
The code is as follows:
// Test
$ V = new VirtualProxy (function (){
Echo 'now, loading', "\ n ";
$ A = new ArrayObject (range (1,100 ));
$ A-> abc = 'a ';
// In actual use, the findXXX method of DataMapper is called here
// The returned result is a collection of domain objects.
Return $;
});
// The proxy object is directly accessed as the original object
// At this time, the callback function passed in by the constructor is called.
// Delay in object loading
Echo $ v-> abc. $ v-> offsetGet (50 );
Virtual Proxy
The code is as follows:
/**
* Virtual proxy: The closure function is called only when the Accessed member is used to generate the target object.
*
* @ Author tonyseek
*
*/
Class VirtualProxy
{
Private $ holder = null;
Private $ loader = null;
/**
* Virtual proxy: The closure function is called only when the Accessed member is used to generate the target object.
*
* @ Param Closure $ loader generates the Closure function of the proxy object
*/
Public function _ construct (Closure $ loader)
{
$ This-> loader = $ loader;
}
/**
* Call of proxy member methods
*
* @ Param string $ method
* @ Param array $ arguments
* @ Throws BadMethodCallException
* @ Return mixed
*/
Public function _ call ($ method, array $ arguments = null)
{
$ This-> check ();
If (! Method_exists ($ this-> holder, $ method )){
Throw new BadMethodCallException ();
}
Return call_user_func_array (
Array (& $ this-> holder, $ method ),
$ Arguments );
}
/**
* Read proxy member attributes
*
* @ Param string $ property
* @ Throws ErrorException
* @ Return mixed
*/
Public function _ get ($ property)
{
$ This-> check ();
If (! Isset ($ this-> holder-> $ property )){
Throw new ErrorException ();
}
Return $ this-> holder-> $ property;
}
/**
* Value assignment of proxy member attributes
*
* @ Param string $ property
* @ Param mixed $ value
*/
Public function _ set ($ property, $ value)
{
$ This-> check ();
$ This-> holder-> $ property = $ value;
}
/**
* Check whether a proxy object already exists. If no proxy object exists, it is generated.
*/
Private function check ()
{
If (null ==$ this-> holder ){
$ Loader = $ this-> loader;
$ This-> holder = $ loader ();
}
}
}
According to the Enterprise Application Architecture Model learned by Dr. Martin, the dynamic language feature of PHP is much easier than Java to implement LazyLoad ). Basic principle...