The goods are learned from the "Enterprise Application Architecture Model" of Martin great God, and the features of the auxiliary PHP Dynamic language can be deferred loaded (lazyload) much more easily than Java. The rationale is that a virtual proxy is used as a placeholder, and once a member (method or property) of the proxy object is accessed, the load is triggered.
But this version of my implementation has limitations:
Applies to objects only, cannot proxy arrays and other basic data types (need to be encapsulated with a arrayobject class of built-in objects)
After being proxied, some interface implementations with operator overloading are invalidated, such as arrayaccess indexers, Itreator iterators, and if this proxy is used to handle deferred loading of collection types, you also need to inherit a subclass to do special processing for external foreach iterations
Demo
Copy the Code code as follows:
Test
$v = new Virtualproxy (function () {
Echo ' Now, Loading ', ' \ n ';
$a = new Arrayobject (range (1,100));
$a->abc = ' a ';
In practice, this is called the Findxxx method of Datamapper.
Returns a collection of domain objects
return $a;
});
The proxy object is accessed directly as the original object
The callback function passed in by the constructor method is called when the
This allows for delay in loading object operations
Echo $v->abc. $v->offsetget (50);
Virtual Proxy
Copy the Code code as follows:
/**
* Virtual proxy, which calls the closure function to generate the target object only when the member is accessed.
*
* @author Tonyseek
*
*/
Class Virtualproxy
{
Private $holder = null;
Private $loader = null;
/**
* Virtual proxy, which calls the closure function to generate the target object only when the member is accessed.
*
* @param Closure $loader generate the closure function of the Proxied object
*/
Public function __construct (Closure $loader)
{
$this->loader = $loader;
}
/**
* Invocation 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 of Agent member properties
*
* @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;
}
/**
* Assignment of Proxy member properties
*
* @param string $property
* @param mixed $value
*/
Public Function __set ($property, $value)
{
$this->check ();
$this->holder-> $property = $value;
}
/**
* Check if the proxy object already exists, and the build does not exist.
*/
Private Function Check ()
{
if (null = = $this->holder) {
$loader = $this->loader;
$this->holder = $loader ();
}
}
}
The above describes the 0x0000008e computer blue screen code in PHP through virtual agent implementation of delay loading implementation code, including the 0x0000008e computer blue screen code content, I hope that the PHP tutorial interested in a friend helpful.