Use virtual proxy in PHP to implement the delayed loading technology. php loading _ PHP Tutorial

Source: Internet
Author: User
In PHP, the virtual proxy is used to implement the delayed loading technology. PHP uses virtual proxy to implement the delayed loading technology. The php loading saying goes from Martin's "Enterprise Application Architecture model" to assist with the dynamic language features of PHP, latency loading technology can be achieved by using virtual proxies in PHP.

This is learned from Martin's Enterprise Application Architecture Model, which assists PHP with the dynamic language features, it is much easier to implement delayed loading than Java-by using a virtual proxy placeholder. The only defect is that only proxy objects are allowed, and basic types cannot be built into the proxy.

In the PHP domain model design I tried to use this to implement delayed loading of DomainObject.

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 ();
}
}
}


// 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 );


How does php implement delayed File loading?

DNT...
The simple code is as follows:
// Important
Echo rand (), 'First come out ';
Ob_flush ();
Flush ();

// Not important...
Include "big. avi ";
Sleep (3 );
Ob_flush ();
?>

From the question you added, I found that the code above was written in white!
Muma ~ As a result, you only need an ajax delay for loading!


How does PHP implement delayed loading?

The delayed loading in php is to load files on demand and instantiate objects on demand.
Loading files on demand is the work of spl_autoload_register. the above implementation can be used for on-demand instantiation, but more will be handled by a proxy loader.

This is learned from Martin's Enterprise Application Architecture Model, which assists PHP with dynamic language features...

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.