zoukankan      html  css  js  c++  java
  • ReflectionClass与Closure

    <?php
    /**
     * Class A
     */
    class A{
    
    }
    
    
    $obj = new ReflectionClass('A');
    var_export($obj.PHP_EOL);
    

    类后面加上PHP_EOL会把当前类的详细接口文档打印出来。

    ReflectionClass 可以利用这个动态创建类,动态使用类方法参数。

    try{
    //如果存在控制器名字的类
    if(class_exists($this->getController())) {
    //利用反射api构造一个控制器类对应的反射类
    $rc = new ReflectionClass($this->getController());
    //如果该类实现 了IController接口
    if($rc->implementsInterface('IController')) {
    //该类拥有解析后的action字符串所指向的方法名
    if($rc->hasMethod($this->getAction())) {
    //构造一个控制器类的实例
    $controller = $rc->newInstance();
    //获取该类$action参数所指向的方法对象
    $method = $rc->getMethod($this->getAction());
    //反射类方法对象的调用方式:
    $method->invoke($controller);
    } else {
    //以下为可能抛出异常
    throw new Exception("Action");
    }
    } else {
    throw new Exception("Interface");
    }
    } else {
    throw new Exception("Controller");
    }
        }catch(exception $e)
        {
            echo $e;
        }
    
    

    Closure //闭包的意思。这个跟javascript的闭包有点类似。

    function getClosure(){
    return function(){};
    }
    var_dump(getClosure());//可以看到是一个 Closure Object
    
  • 相关阅读:
    小程序云函数 Error: errCode: -404011 cloud function execution error
    Fiddler+雷电模拟器进行APP抓包
    Vue优化常用技巧
    sftp常用命令整理
    React生命周期学习整理
    git 提交跳过检查
    漂亮的代码系列
    关于我系列
    架构系列
    深度学习系列
  • 原文地址:https://www.cnblogs.com/canbefree/p/5212531.html
Copyright © 2011-2022 走看看