• 反射类及应用


    $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() # 检查一个类/对象是否定义某个方法

  • 相关阅读:
    pycharm运行html文件报404错误
    css3 鼠标悬浮动画效果
    子代选择器和后代选择器的区别
    前端入门
    爬虫Scrapy框架
    BeautifulSoup
    爬虫之selenium使用
    爬虫之BeautifulSoup
    urllib模块
    爬虫基础
  • 原文地址:https://www.cnblogs.com/sien6/p/13780040.html
Copyright © 2020-2023  润新知