Using virtual proxy in PHP to implement lazy loading technology, php loading_PHP tutorial

WBOY
Release: 2016-07-13 10:15:09
Original
839 people have browsed it

Use virtual proxy in PHP to implement lazy loading technology, PHP loading

It is said that this guy learned it from Martin’s "Enterprise Application Architecture Patterns". It assists the characteristics of PHP dynamic language and can implement lazy loading much easier than Java - through a virtual proxy placeholder. The only flaw is that it can only proxy objects, not built-in basic types.

In the PHP domain model design I tried, I also used this to implement lazy loading of DomainObject.

Copy code The code is as follows:

* Virtual proxy, the closure function is called to generate the target object only when the member is accessed.
*
* @author tonyseek
*
*/
class VirtualProxy
{
private $holder = null;
private $loader = null;

/**
* Virtual proxy, the closure function is called to generate the target object only when the member is accessed.
*
* @param Closure $loader generates the closure function of the proxy object
​​*/
Public function __construct(Closure $loader)
{
$this->loader = $loader;
}

/**
* * Calling 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);
}

/**
* Reading of 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;
}

/**
* Assignment of proxy member attributes
*
* @param string $property
* @param mixed $value
​​*/
Public function __set($property, $value)
{
                $this->check();

$this->holder->$property = $value;
}

/**
* Check whether the proxy object already exists, and generate it if it 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 actual use, the findXXX method of DataMapper is called here
​​​​ //Returns a collection of domain objects
Return $a;
});
// The proxy object is directly accessed as the original object
// At this time, the callback function passed in by the constructor is called
//Thus achieving the delay of loading object operation
echo $v->abc . $v->offsetGet(50);

How to delay loading a file in php

Push the content displayed first out of the buffer. Whether the subsequent content comes out will not affect the previous content. . .
The simple code is as follows:
//Important
echo rand(),'The one that came out first
';
ob_flush();
flush();

//Not important. . .
include "big.avi";
sleep(3);
ob_flush();
?>

From the questions you added, I found that my code above It’s all in vain!
Ni Ma~ But you only need an ajax delayed load!

How to implement lazy loading in PHP?

Lazy loading in PHP is ultimately about loading files on demand and instantiating objects on demand.
Loading files on demand is the job of spl_autoload_register. On-demand instantiation can be implemented using the above, but more Will use a proxy loader to handle

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/906675.htmlTechArticleVirtual proxy is used in PHP to implement lazy loading technology. PHP loading is said to be from Martin's "Enterprise Application Architecture" The features learned in "Model" to assist PHP dynamic language can be compared to...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template