zoukankan      html  css  js  c++  java
  • 反射

    <?php
    class HelloWorld {
    public function sayHelloTo($name) {
    return 'Hello ' . $name;
    }
    }
    $reflectionMethod = new ReflectionMethod('HelloWorld', 'sayHelloTo');
    echo $reflectionMethod->invoke(new HelloWorld(), 'Mike');
    ?> 
    以上例程会输出:
    Hello Mike

    thinkphp 控制器调度详解-ReflectionClass,ReflectionMethod

     1.如何通过地址栏参数来得到模块名称和控制器名称(即使在有路由和开了重写模块的情况下) 

                在之前的博文中已经有了比较详细的说明:http://my.oschina.net/u/1178986/blog/201206003 

      2.tp是如何实现前置,后置方法功能模块,和如何执行带参数的方法?

                php系统自带的 ReflectionClass,ReflectionMethod 类,可以反射用户自定义类的中属性,方法的权限和参数等信息,通过这些信息可以准确的控制方法的执行 

                 ReflectionClass:  详情

                                    主要用的方法:  hasMethod(string)  是否存在某个方法

                                                          getMethod(string)   获取方法

                 ReflectionMethod : 详情

                                    主要方法:  getNumberOfParameters()  获取参数个数

                                                    getParamters()  获取参数信息

       3.代码演示

                 

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    <?php   
    class IndexAction{
        public function index(){
                echo 'index'." ";
        }
        public function test($year=2012,$month=2,$day=21){
             echo $year.'--------'.$month.'-----------'.$day." ";
        }
        public function _before_index(){
                echo __FUNCTION__." ";
        }
        public function _after_index(){
                echo __FUNCTION__." ";
        }
    }
     
    //执行index方法
    $method new ReflectionMethod('IndexAction','index');
    //进行权限判断
    if($method->isPublic()){
        $class new ReflectionClass('IndexAction');
        //执行前置方法
        if($class->hasMethod('_before_index')){
            $beforeMethod $class->getMethod('_before_index');
            if($beforeMethod->isPublic()){
                $beforeMethod->invoke(new IndexAction);
            }
        }
     
        $method->invoke(new IndexAction);
     
        //执行后置方法
        if($class->hasMethod('_after_index')){
            $beforeMethod $class->getMethod('_after_index');
            if($beforeMethod->isPublic()){
                $beforeMethod->invoke(new IndexAction);
            }
        }
    }
     
     
    //执行带参数的方法
    $method new ReflectionMethod('IndexAction','test');
    $params $method->getParameters();
    foreach($params as $param ){
        $paramName $param->getName();
        if(isset($_REQUEST[$paramName]))
            $args[] = $_REQUEST[$paramName];
        elseif($param->isDefaultValueAvailable())
            $args[] = $param->getDefaultValue();
    }
    if(count($args)==$method->getNumberOfParameters())
        $method->invokeArgs(new IndexAction,$args);
    else
        echo 'parameters is not match!';

    运行结果: 

               

  • 相关阅读:
    使用hibernate利用实体类生成表和利用表生成实体类
    多线程循环打印ABC
    maven在整合springmvc+hibernate运行时遇到的一些问题
    checkbox属性获取
    glib中关于线程池的一个实例
    阅读英文文献总结的专业词汇
    网络流分类领域牛人
    锐捷s3550千兆交换机配置端口镜像
    转载Wireshark过滤语法
    DispatcherServlet处理流程
  • 原文地址:https://www.cnblogs.com/hellowzd/p/4657843.html
Copyright © 2011-2022 走看看