PHP中使用虛代理實現消極式載入技術,php載入_PHP教程

來源:互聯網
上載者:User

PHP中使用虛代理實現消極式載入技術,php載入


話說這貨是從 Martin 大神的《公司專屬應用程式架構模式》中學到的,輔助 PHP 動態語言的特性,可以比 Java 輕鬆很多的實現消極式載入——通過一個虛代理預留位置。唯一的缺陷,是只能代理對象,不能代理內建基本類型。

我試水的 PHP 領域模型設計中,也是用這個來實現 DomainObject 的消極式載入。

複製代碼 代碼如下:
* 虛代理,只有在被訪問成員時才調用閉包函數產生目標對象。
*
* @author tonyseek
*
*/
class VirtualProxy
{
private $holder = null;
private $loader = null;

/**
* 虛代理,只有在被訪問成員時才調用閉包函數產生目標對象。
*
* @param Closure $loader 產生被代理對象的閉包函數
*/
public function __construct(Closure $loader)
{
$this->loader = $loader;
}

/**
* 代理成員方法的調用
*
* @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);
}

/**
* 代理成員屬性的讀取
*
* @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;
}

/**
* 代理成員屬性的賦值
*
* @param string $property
* @param mixed $value
*/
public function __set($property, $value)
{
$this->check();

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

/**
* 檢查是否已經存在被代理對象,不存在則產生。
*/
private function check()
{
if (null == $this->holder) {
$loader = $this->loader;
$this->holder = $loader();
}
}
}


// 測試
$v = new VirtualProxy(function(){
echo 'Now, Loading', "\n";
$a = new ArrayObject(range(1,100));
$a->abc = 'a';
// 實際使用中,這裡調用的是 DataMapper 的 findXXX 方法
// 返回的是領域對象集合
return $a;
});
// 代理對象直接當作原對象訪問
// 而此時構造方法傳入的 callback 函數才被調用
// 從而實現載入對象操作的延遲
echo $v->abc . $v->offsetGet(50);


php 怎實現消極式載入某檔案

把先顯示的內容衝出緩衝區,後面的內容出不出來都不會影響到前面的內容...
簡單的代碼如下:
//重要的
echo rand(),'先出來的';
ob_flush();
flush();

//不重要的...
include "big.avi";
sleep(3);
ob_flush();
?>

從你補充的問題中,我發覺我上面的代碼白寫了!
泥馬~結果你卻只要一個ajax消極式載入!

 

PHP怎實現消極式載入?

php裡面的消極式載入說到底就是按需負載檔案,按需執行個體化對象這兩個部分
按需負載檔案是spl_autoload_register的工作,按需執行個體化可以用以上的實現,不過更多會用一個代理載入器來處理
 

http://www.bkjia.com/PHPjc/906675.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/906675.htmlTechArticlePHP中使用虛代理實現消極式載入技術,php載入 話說這貨是從 Martin 大神的《公司專屬應用程式架構模式》中學到的,輔助 PHP 動態語言的特性,可以比...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.