search

PHP反射API

Jun 13, 2016 am 10:52 AM
apiphpaboutanalyzereflectionExportExpandstateunderstandofprogramrun

反射的理解
它是指在php的运行状态中,扩展分析php程序,导出或者提取出关于类、方法、属性、参数等详细信息,甚至包括注释。这种动态获取的信息以及动态调用对象的方法的功能称为反射API。反射是操纵面向对象模型中元模型的API,其功能十分强大,可以帮助我们构建复杂,可扩展的应用。(ps:包括在工厂模式中的使用)
反射API是php内建的oop技术扩展,包括一些类、异常和接口,综合使用他们可用来帮助我们分析其它类,接口,方法,属性和扩展。这些oop扩展被称为反射。
ReflectionClass
[php] 
class ReflectionClass implements Reflector 

    final private __clone() 
    public object __construct(string name) 
    public string __toString() 
    public static string export() 
    //导出该类的详细信息 
    public string getName() 
    //取得类名或接口名 
    public bool isInternal() 
    //测试该类是否为系统内部类 
    public bool isUserDefined() 
    //测试该类是否为用户自定义类 
    public bool isInstantiable() 
    //测试该类是否被实例化过 
    public bool hasConstant(string name) 
    //测试该类是否有特定的常量 
    public bool hasMethod(string name) 
    //测试该类是否有特定的方法 
    public bool hasProperty(string name) 
    //测试该类是否有特定的属性 
    public string getFileName() 
    //取得定义该类的文件名,包括路径名 
    public int getStartLine() 
    //取得定义该类的开始行 
    public int getEndLine() 
    //取得定义该类的结束行 
    public string getDocComment() 
    //取得该类的注释 
    public ReflectionMethod getConstructor() 
    //取得该类的构造函数信息 
    public ReflectionMethod getMethod(string name) 
    //取得该类的某个特定的方法信息 
    public ReflectionMethod[] getMethods() 
    //取得该类的所有的方法信息 
    public ReflectionProperty getProperty(string name) 
    //取得某个特定的属性信息 
    public ReflectionProperty[] getProperties() 
    //取得该类的所有属性信息 
    public array getConstants() 
    //取得该类所有常量信息 
    public mixed getConstant(string name) 
    //取得该类特定常量信息 
    public ReflectionClass[] getInterfaces() 
    //取得接口类信息 
    public bool isInterface() 
    //测试该类是否为接口 
    public bool isAbstract() 
    //测试该类是否为抽象类 
    public bool isFinal() 
    //测试该类是否声明为final 
    public int getModifiers() 
    //取得该类的修饰符,返回值类型可能是个资源类型 
    //通过Reflection::getModifierNames($class->getModifiers())进一步读取 
    public bool isInstance(stdclass object) 
    //测试传入的对象是否为该类的一个实例 
    public stdclass newInstance(mixed* args) 
    //创建该类实例 
    public ReflectionClass getParentClass() 
    //取得父类 
    public bool isSubclassOf(ReflectionClass class) 
    //测试传入的类是否为该类的父类 
    public array getStaticProperties() 
    //取得该类的所有静态属性 
    public mixed getStaticPropertyValue(string name [, mixed default]) 
    //取得该类的静态属性值,若private,则不可访问 
    public void setStaticPropertyValue(string name, mixed value) 
    //设置该类的静态属性值,若private,则不可访问,有悖封装原则 
    public array getDefaultProperties() 
    //取得该类的属性信息,不含静态属性 
    public bool isIterateable() 
    public bool implementsInterface(string name) 
    //测试是否实现了某个特定接口 
    public ReflectionExtension getExtension() 
    public string getExtensionName() 

?> 
工厂模式应用:
[php] 
class MoveDataFactory 

    /**
     * Description:简单工厂模式,根据mode选取不同实例化对象
     * @return 对象实例
     */ 
    public function GetMoveClass($classname) 
    { 
        $reflectionclass = new ReflectionClass($classname); 
        return $reflectionclass->newInstance(); 
    } 

 

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment