详解PHP的反射使用-php教程

资源魔 41 0

上面咱们讲下反射正在实际开发中的使用。

  • 主动天生文档
  • 完成 MVC 架构
  • 完成单位测试
  • 合营 DI 容器处理依赖

主动天生文档

依据反射的剖析类,接口,函数以及办法的外部构造,办法以及函数的参数,和类的属性以及办法,能够主动天生文档。

/**
 * 先生类
 *
 * 形容信息
 */
class Student
{
    const NORMAL = 1;
    const FORBIDDEN = 2;
    /**
     * 用户ID
     * @var 类型
     */
    public $id;
    /**
     * 猎取id
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }
    public function setId($id = 1)
    {
        $this->id = $id;
    }
}
$ref = new ReflectionClass('Student');
$doc = $ref->getDocCo妹妹ent();
echo $ref->getName() . ':' . getCo妹妹ent($ref) , "\n";
echo "属性列表:\n";
printf("%-15s%-10s%-40s\n", 'Name', 'Access', 'Co妹妹ent');
$attr = $ref->getProperties();
foreach ($attr as $row) {
    printf("%-15s%-10s%-40s\n", $row->getName(), getAccess($row), getCo妹妹ent($row));
}
echo "常量列表:\n";
printf("%-15s%-10s\n", 'Name', 'Value');
$const = $ref->getConstants();
foreach ($const as $key => $val) {
    printf("%-15s%-10s\n", $key, $val);
}
echo "\n\n";
echo "办法列表\n";
printf("%-15s%-10s%-30s%-40s\n", 'Name', 'Access', 'Params', 'Co妹妹ent');
$methods = $ref->getMethods();
foreach ($methods as $row) {
    printf("%-15s%-10s%-30s%-40s\n", $row->getName(), getAccess($row), getParams($row), getCo妹妹ent($row));
}
// 猎取权限
function getAccess($method)
{
    if ($method->isPublic()) {
        return 'Public';
    }
    if ($method->isProtected()) {
        return 'Protected';
    }
    if ($method->isPrivate()) {
        return 'Private';
    }
}
// 猎取办法参数信息
function getParams($method)
{
    $str = '';
    $parameters = $method->getParameters();
    foreach ($parameters as $row) {
        $str .= $row->getName() . ',';
        if ($row->isDefaultValueAvailable()) {
            $str .= "Default: {$row->getDefaultValue()}";
        }
    }
    return $str ? $str : '';
}
// 猎取正文
function getCo妹妹ent($var)
{
    $co妹妹ent = $var->getDocCo妹妹ent();
    // 简略的猎取了第一行的信息,这里能够自行扩大
    preg_match('/\* (.*) *?/', $co妹妹ent, $res);
    return isset($res[1]) ? $res[1] : '';
}

运转 php file.php 就能够看到相应的文档信息。

完成 MVC 架构

如今很多多少框架都是 MVC 的架构,依据路由信息定位 管制器($controller) 以及办法($method) 的称号,之后应用反射完成主动挪用。

$class = new ReflectionClass(ucfirst($controller) . 'Controller');
$controller = $class->newInstance();
if ($class->hasMethod($method)) {
    $method = $class->getMethod($method);
    $method->invokeArgs($controller, $arguments);
} else {
    throw new Exception("{$controller} controller method {$method} not exists!");
}

完成单位测试

普通状况下咱们会对函数以及类进行测试,判别其能否可以按咱们预期前往后果,咱们能够用反射完成一个简略通用的类测试用例。

class Calc
{
    public function plus($a, $b)
    {
        return $a + $b;
    }
    public function minus($a, $b)
    {
        return $a - $b;
    }
}
function testEqual($method, $assert, $data)
{
    $arr = explode('@', $method);
    $class = $arr[0];
    $method = $arr[1];
    $ref = new ReflectionClass($class);
    if ($ref->hasMethod($method)) {
        $method = $ref->getMethod($method);
        $res = $method->invokeArgs(new $class, $data);
        var_dump($res === $assert);
    }
}
testEqual('Calc@plus', 3, [1, 2]);
testEqual('Calc@minus', -1, [1, 2]);

这是类的测试办法,也能够行使反射完成函数的测试办法。
这里只是我简略写的一个测试用例,PHPUnit 单位测试框架很年夜水平上依赖了 Reflection 的特点,能够理解下。

合营 DI 容器处理依赖

Laravel 等许多框架都是应用 Reflection 处理依赖注入成绩,详细可查看 Laravel 源码进行剖析。
上面咱们代码简略完成一个 DI 容器演示 Reflection 处理依赖注入成绩。

class DI
{
    protected static $data = [];
    public function __set($k, $v)
    {
        self::$data[$k] = $v;
    }
    public function __get($k)
    {
        return $this->bulid(self::$data[$k]);
    }
    // 猎取实例
    public function bulid($className)
    {
        // 假如是匿名函数,间接执行,并前往后果
        if ($className instanceof Closure) {
            return $className($this);
        }
        
        // 曾经是实例化工具的话,间接前往
        if(is_object($className)) {
            return $className;
        }
        // 假如是类的话,应用反射加载
        $ref = new ReflectionClass($className);
        // 监测类能否可实例化
        if (!$ref->isInstantiable()) {
            throw new Exception('class' . $className . ' not find');
        }
        // 猎取结构函数
        $construtor = $ref->getConstructor();
        // 无结构函数,间接实例化前往
        if (is_null($construtor)) {
            return new $className;
        }
        // 猎取结构函数参数
        $params = $construtor->getParameters();
        // 解析结构函数
        $dependencies = $this->getDependecies($params);
        // 创立新实例
        return $ref->newInstanceArgs($dependencies);
    }
    // 剖析参数,假如参数中呈现依赖类,递归实例化
    public function getDependecies($params)
    {
        $data = [];
        foreach($params as $param)
        {
            $tmp = $param->getClass();
            if (is_null($tmp)) {
                $data[] = $this->setDefault($param);
            } else {
                $data[] = $this->bulid($tmp->name);
            }
        }
        return $data;
    }
    
    // 设置默许值
    public function setDefault($param)
    {
        if ($param->isDefaultValueAvailable()) {
            return $param->getDefaultValue();
        }
        throw new Exception('no default value!');
    }
}
class Demo
{
    public function __construct(Calc $calc)
    {
        echo $calc->plus(1, 2);
    }
}
$di = new DI();
$di->calc = 'Calc'; // 加载单位测试用例中 Calc 类
$di->demo = 'Demo';
$di->demo;

留意下面的 calc 以及 demo 的程序,不克不及倒置,否则的话会报错,缘由是因为 Demo 依赖 Calc,起首要界说依赖关系。
正在 Demo 实例化的时分,会用到 Calc 类,也就是说 Demo 依赖于 Calc,然而正在 $data 下面找没有到的话,会抛犯错误,以是起首要界说 $di->calc = 'Calc'

Reflection 是一个十分 Cool 的性能,应用它,但没有要滥用它。

End

坚持原创技巧分享,您的支持将激励我持续

保举教程:《php教程》

以上就是详解PHP的反射应用的具体内容,更多请存眷资源魔其它相干文章!

标签: php php开发教程 php开发资料 php开发自学

上一个最全的php数组操作方法汇总,让你轻松掌握!-php教程

下一个PHP实战之Redis常见7种使用场景-php教程

抱歉,评论功能暂时关闭!