as the actual loading of the BLOB.
*/
class ImageProxy extends AbstractImage
{
public function __construct($path)
{
$this->_path = $path;
list ($this->_width, $this->_height) = getimagesize($path);
}
/**
* Creates a RawImage and exploits its functionalities.
*/
protected function _lazyLoad()
{
if ($this->_realImage === null) {
$this->_realImage = new RawImage($this->_path);
}
}
public function dump()
{
$this->_lazyLoad();
return $this->_realImage->dump();
}
}
/**
* Client class that does not use the data dump of the image.
* Passing blindly a Proxy to this class and to other Clients makes sense
* as the data would be loaded anyway when Image::dump() is called.
*/
class Client
{
public function tag(Image $img)
{
return '';
}
}
$path = ''/home/giorgio/shared/Immagini/kiki.png'';
$client = new Client();
$image = new RawImage($path); // loading of the BLOB takes place
echo $client->tag($image), "\n";
$proxy = new ImageProxy($path);
echo $client->tag($proxy), "\n"; // loading does not take place even here
以上代码实现了PHP的代理模式。简单来讲,代理模式就是为其他对象提供一个代理以控制对这个对象的访问。