PHP load _php tutorial using virtual proxy for lazy loading technology

Source: Internet
Author: User

PHP uses virtual proxy to implement lazy loading technology, PHP load


The article is from the "Enterprise Application Architecture Model" of Martin great God, the features of the auxiliary PHP Dynamic language can be deferred-loaded with a much easier implementation than Java-through a virtual proxy placeholder. The only flaw is that you can only proxy objects and cannot proxy built-in basic types.

I try the water of the PHP domain model design, but also with this to achieve domainobject delay loading.

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


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


How PHP implements lazy loading of a file

The first display of the content out of the buffer, the contents of the back will not be affected by the previous content ...
The simple code is as follows:
Important to
echo rand (), ' first 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!
Mud Horse ~ As a result you just need an AJAX lazy load!


How does php implement lazy loading?

The lazy load inside PHP is basically loading files on demand, instantiating objects on demand two parts
Loading files On demand is the work of spl_autoload_register, on-demand instantiation can be implemented with the above, but more will be used by a proxy loader to handle

http://www.bkjia.com/PHPjc/906675.html www.bkjia.com true http://www.bkjia.com/PHPjc/906675.html techarticle using Virtual Agent in PHP to implement lazy loading technology, PHP load say this is from the "Enterprise Application Architecture Model" of Martin great God, auxiliary php Dynamic language features, can compare ...

  • Related Article

    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.