web编程之路PHP实战程序猿阵线联盟-汇总各类技术干货

php一个简易 的异步加载类

2016-10-09  本文已影响93人  hopevow

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;

输出 :

结果
上一篇 下一篇

猜你喜欢

热点阅读