zoukankan      html  css  js  c++  java
  • 反射类及应用

    $class = new ReflectionClass('Person');
    $class ->isInstantiable() ## 判断是否可以被实例化
    $class ->getConstructor() ## 获取类的构造函数
    $constructor->getParameters(); ## 返回构造函数参数
    1.常量 Contants
    2.属性 Property Names
    $properties = $class->getProperties();
    foreach ($properties as $property) {
    echo $property->getName() . " "; //可以得到属性名
    }
    如果只想获取到private属性,就要额外传个参数:$private_properties = $class->getProperties(ReflectionProperty::IS_PRIVATE);
    3.方法 Method Names静态
    getMethods() 来获取到类的所有methods。
    hasMethod(string) 是否存在某个方法
    getMethod(string) 获取方法
    --$method = $class->getmethod('getName'); // 获取Person 类中的getName方法
    --$method->invoke($instance); // 执行getName 方法
    --$method = $class->getmethod('setName'); // 获取Person 类中的setName方法
    --$method->invokeArgs($instance, array('snsgou.com'));
    4.属性 Static Properties
    5.命名空间 Namespace
    6.Person类是否为final或者abstract
    7.Person类是否有某个方法
     
    内省函数
    class_exists() # 检查一个类是否被定义
    get_class() # 返回对象的类名
    get_parent_class() # 返回对象的类的父类名
    is_subclass_of() # 检查一个对象是否是给定父类的 子类
    ###
    get_declared_classes() # 返回一系列已经定义的类
    get_class_methods() # 返回给定【类|对象】的所有方法 private 和 protected 方法会被跳过
    get_class_vars() # 返回当前【类名】的默认属性 private 和 protected 属性会被跳过
    interface_exists() # 检查某个【接口名】是否 被定义
    method_exists() # 检查一个类/对象是否定义某个方法

  • 相关阅读:
    centos 下源码安装postgresql 9.4
    sql server访问excel文件
    什么是脏读、不可重复读、幻读
    对象名 master.dbo.spt_values' 无效
    数据库恢复和日志文件阐述
    查看数据大小和日志文件大小
    CPU性能瓶颈
    数据库打补丁,服务升级失败
    Docker安装WordPress并添加https访问
    svn插件安装
  • 原文地址:https://www.cnblogs.com/sien6/p/13780040.html
Copyright © 2011-2022 走看看