PHP框架类加载器

 ̄綄美尐妖づ 提交于 2019-12-19 07:09:12

今天跟着教程一块走了一个 加载各种类的加载器,基于面向消息的思路,向队列添加任务消息;具体跟着代码走一遍 就可以了.感觉不是很难

<?php
class A
{
    public static function testA($a)
    {
        echo 'A' . $a;
        return false;
    }
}

//静态方法
class B
{
    public static function testB($b)
    {
        echo 'B' . $b;
        return false;
    }
}

//对象方法
class C
{
    public function testC($c)
    {
        echo 'C' . $c;
        return false;
    }
}

//单例方法
/*程序执行完成有0个或者1个实例*/

class D
{
    private static $d = Null;

    private function __construct()
    {
        echo 123;
    }

    public static function newInstance()
    {
        if (self::$d == Null) {
            self::$d = new D();
        }
        return self::$d;
    }

    public function testD($d)
    {
        echo "D" . $d;
    }
}


class Core
{
    //方法类型常量
    const __OBJECT__ = 1;
    const __STATIC__ = 2;
    const __SINGLE__ = 3;
    //消息数组
    private static $actList = [];

    //消息监听
    public static function listen()
    {
        if (count(self::$actList)) {
            return true;
        } else {
            return false;
        }
    }

    //消息头添加
    public static function unshift($className, $functionName, $param = [], $type)
    {
        array_unshift(self::$actList, [$className, $functionName, $param, $type]);
    }

    //消息尾添加
    public static function push($className, $functionName, $param = [], $type)
    {
        array_push(self::$actList, [$className, $functionName, $param, $type]);
    }

    //类加载器
    public static function doAct()
    {
        $arr = array_shift(self::$actList);
        $className = $arr[0];
        $functionName = $arr[1];
        $param = $arr[2];
        $type = $arr[3];
        if ($type == self::__STATIC__) {
            call_user_func_array([$className, $functionName], $param);
        } else if ($type == self::__OBJECT__) {

            $object = new $className();
            call_user_func_array([$object, $functionName], $param);

        } else if ($type == self::__SINGLE__) {
            $object = $className::newInstance();
            call_user_func_array([$object, $functionName], $param);
        }

    }
}

Loader::unshift("B", 'testB', [1], Loader::__STATIC__);
Loader::push("C", 'testC', [1], Loader::__OBJECT__);
Loader::push("D", 'testD', [1], Loader::__SINGLE__);
while (Loader::listen()) {
    Loader::doAct();
}


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!