php一个简易 的异步加载类
<?php
/**
* 异步加载
*/
class _async {
private $func = false;
private $arg = false;
private $data = false;
function __construct($func, $arg) {
$this->func = $func;
$this->arg = $arg;
}
function __get($name) {
if ($this->data === false) {
$this->data = call_user_func_array($this->func, $this->arg);
}
return $this->data->$name;
}
}
<?php
require_once "./util/async.class.php";
class Goods {
public $g1 = 'hello world';
public $g2;
function __construct($str) {
$this->g2 = $str;
}
}
class Dev {
public $d1 = 'this is d1';
public $d2 = 'this is d2';
function __construct($str) {
$this->goods = new _async(function($arg){return new Goods($arg);}, [$str]);
}
}
$dev = new Dev('dev\'s arg to goods');
echo $dev->goods->g1,'<br/>',$dev->goods->g2;
输出 :
结果
网友评论