Implementation of delay loading by virtual Proxy in PHP code _php skills

Source: Internet
Author: User
Tags closure mixed
This is from Martin God's "Enterprise Application architecture Model" learned that the auxiliary PHP Dynamic language features, can be much easier than Java implementation of lazy Load (lazyload). The basic principle is to use a virtual proxy as a placeholder, and once you access a member of the proxy object (method or property), the load is triggered.

However, this version of my implementation has limitations:

Only applies to objects, cannot proxy arrays, and other basic data types (needs to be encapsulated with a arrayobject type of built-in objects)
After being represented, some interface implementations with operator overloading properties are invalidated, such as arrayaccess indexers, Itreator iterators, and if the proxy is used to handle deferred loading of collection types, it is also necessary to inherit a subclass for special processing so that the external use of the foreach iteration
Demo
Copy Code code as follows:

Test
$v = new Virtualproxy (function () {
Echo ' Now, Loading ', "\ n";
$a = new Arrayobject (range (1,100));
$a->abc = ' a ';
In actual use, this is called the Datamapper findxxx method.
Returns a collection of Realm objects
return $a;
});
Proxy object is accessed directly as the original object
The callback function passed in by the constructor is then invoked
Thus implementing the delay in loading object operations
Echo $v->abc. $v->offsetget (50);

Virtual Proxy
Copy 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 to generate closure functions for the proxy object
*/
Public function __construct (Closure $loader)
{
$this->loader = $loader;
}

/**
* Invocation of Proxy member method
*
* @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);
}

/**
* Agent member properties read
*
* @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 to see if the proxy object already exists and is generated if it does not exist.
*/
Private Function Check ()
{
if (null = = $this->holder) {
$loader = $this->loader;
$this->holder = $loader ();
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.