search
HomeBackend DevelopmentPHP TutorialDetailed explanation of PHP reflection mechanism and plug-in architecture examples

1. Purpose:
This extension analyzes PHP programs and exports or extracts detailed information about classes, methods, properties, parameters, etc., including comments.
Reflection can be said to be an extension of the PHP library function: "Classes/Objects class/object function".
Mainly used to detect information about classes, methods and other information inside existing PHP programs through programs and process them.

2. API Overview:

class Reflection { }
interface Reflector { }
class ReflectionException extends Exception { }
class ReflectionFunction implements Reflector { }
class ReflectionParameter implements Reflector { }
class ReflectionMethod extends ReflectionFunction { }
class ReflectionClass implements Reflector { }
class ReflectionObject extends ReflectionClass { }
class ReflectionProperty implements Reflector { }
class ReflectionExtension implements Reflector { }

3. Detailed description: (See the php manual for examples)
①Reflection class

<?php
class Reflection
{
    public static mixed export(Reflector r [,bool return])
    //导出一个类或方法的详细信息
    public static array getModifierNames(int modifiers)
    //取得修饰符的名字
}
?>

②ReflectionException class

This class inherits the standard class and has no special methods and attributes.

③ReflectionFunction class

<?php
class ReflectionFunction 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 string getFileName()
    //取得文件名,包括路径名
    public int getStartLine()
    //取得定义函数的起始行
    public int getEndLine()
    //取得定义函数的结束行
    public string getDocComment()
    //取得函数的注释
    public array getStaticVariables()
    //取得静态变量
    public mixed invoke(mixed* args)
    //调用该函数,通过参数列表传参数
    public mixed invokeArgs(array args)
    //调用该函数,通过数组传参数
    public bool returnsReference()
    //测试该函数是否返回引用
    public ReflectionParameter[] getParameters()
    //取得该方法所需的参数,返回值为对象数组
    public int getNumberOfParameters()
    //取得该方法所需的参数个数
    public int getNumberOfRequiredParameters()
    //取得该方法所需的参数个数
}
?>

④ReflectionParameter class:

<?php
class ReflectionParameter implements Reflector
{
    final private clone()
    public object construct(string name)
    public string toString()
    public static string export()
    //导出该参数的详细信息
    public string getName()
    //取得参数名
    public bool isPassedByReference()
    //测试该参数是否通过引用传递参数
    public ReflectionClass getClass()
    //若该参数为对象,返回该对象的类名
    public bool isArray()
    //测试该参数是否为数组类型
    public bool allowsNull()
    //测试该参数是否允许为空
    public bool isOptional()
    //测试该参数是否为可选的,当有默认参数时可选
    public bool isDefaultValueAvailable()
    //测试该参数是否为默认参数
    public mixed getDefaultValue()
    //取得该参数的默认值
}
?>

⑤ReflectionClass class:

<?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()
}
?>

⑥ReflectionMethod class:

<?php
class ReflectionMethod extends ReflectionFunction
{
    public construct(mixed class, string name)
    public string toString()
    public static string export()
    //导出该方法的信息
    public mixed invoke(stdclass object, mixed* args)
    //调用该方法
    public mixed invokeArgs(stdclass object, array args)
    //调用该方法,传多参数
    public bool isFinal()
    //测试该方法是否为final
    public bool isAbstract()
    //测试该方法是否为abstract
    public bool isPublic()
    //测试该方法是否为public
    public bool isPrivate()
    //测试该方法是否为private
    public bool isProtected()
    //测试该方法是否为protected
    public bool isStatic()
    //测试该方法是否为static
    public bool isConstructor()
    //测试该方法是否为构造函数
    public bool isDestructor()
    //测试该方法是否为析构函数
    public int getModifiers()
    //取得该方法的修饰符
    public ReflectionClass getDeclaringClass()
    //取得该方法所属的类
    // Inherited from ReflectionFunction
    final private clone()
    public string getName()
    public bool isInternal()
    public bool isUserDefined()
    public string getFileName()
    public int getStartLine()
    public int getEndLine()
    public string getDocComment()
    public array getStaticVariables()
    public bool returnsReference()
    public ReflectionParameter[] getParameters()
    public int getNumberOfParameters()
    public int getNumberOfRequiredParameters()
}
?>

⑦ReflectionProperty class:

<?php
class ReflectionProperty implements Reflector
{
    final private clone()
    public construct(mixed class, string name)
    public string toString()
    public static string export()
    //导出该属性的详细信息
    public string getName()
    //取得该属性名
    public bool isPublic()
    //测试该属性名是否为public
    public bool isPrivate()
    //测试该属性名是否为private
    public bool isProtected()
    //测试该属性名是否为protected
    public bool isStatic()
    //测试该属性名是否为static
    public bool isDefault()
    public int getModifiers()
    //取得修饰符
    public mixed getValue(stdclass object)
    //取得该属性值
    public void setValue(stdclass object, mixed value)
    //设置该属性值
    public ReflectionClass getDeclaringClass()
    //取得定义该属性的类
    public string getDocComment()
    //取得该属性的注释
}
?>

⑧ReflectionExtension class

<?php
class ReflectionExtension implements Reflector {
    final private clone()
    public construct(string name)
    public string toString()
    public static string export()
    //导出该扩展的所有信息
    public string getName()
    //取得该扩展的名字
    public string getVersion()
    //取得该扩展的版本
    public ReflectionFunction[] getFunctions()
    //取得该扩展的所有函数
    public array getConstants()
    //取得该扩展的所有常量
    public array getINIEntries()
    //取得与该扩展相关的,在php.ini中的指令信息
    public ReflectionClass[] getClasses()
    public array getClassNames()
}
?>

4. Attachment:

In fact, it can be seen from the second API overview: the interface is very easy to use.
On the one hand, the Reflector interface provides a good interface naming specification for the Reflection small system.
Every class that implements it must follow its specifications. From the outside, the API of this small system is very unified.
On the other hand, since many classes implement the Reflector interface,
In such a class hierarchy, it is very easy to implement polymorphism.

PHP Reflection API--Plug-in system architecture implemented using reflection technology

/** 
* @name    PHP反射API--利用反射技术实现的插件系统架构 
* @author :PHPCQ.COM 
*/    
interface Iplugin{    
                 public static function getName();    
 }    
function findPlugins(){    
                $plugins = array();    
                foreach (get_declared_classes() as $class){    
                                 $reflectionClass = new ReflectionClass($class);    
                                 if ($reflectionClass->implementsInterface(&#39;Iplugin&#39;)) {    
                                                 $plugins[] = $reflectionClass;    
                                 }    
                 }    
                  return $plugins;    
  }    
 function computeMenu(){    
                 $menu = array();    
                 foreach (findPlugins() as $plugin){    
                                 if ($plugin->hasMethod(&#39;getMenuItems&#39;)) {    
                                                 $reflectionMethod = $plugin->getMethod(&#39;getMenuItems&#39;);    
                                                 if ($reflectionMethod->isStatic()) {    
                                                                $items = $reflectionMethod->invoke(null);    
                                                } else {    
                                                                 $pluginInstance = $plugin->newInstance();    
                                                                 $items = $reflectionMethod->invoke($pluginInstance);    
                                                 }    
                                                 $menu = array_merge($menu,$items);    
                                 }    
                 }    
                return $menu;    
 }    
 function computeArticles(){    
                 $articles = array();    
                 foreach (findPlugins() as $plugin){    
                                 if ($plugin->hasMethod(&#39;getArticles&#39;)) {    
                                                $reflectionMethod = $plugin->getMethod(&#39;getArticles&#39;);    
                                                if ($reflectionMethod->isStatic()) {    
                                                                 $items = $reflectionMethod->invoke(null);    
                                                 } else {    
                                                                 $pluginInstance = $plugin->newInstance();    
                                                                 $items = $reflectionMethod->invoke($pluginInstance);    
                                                 }    
                                                 $articles = array_merge($articles,$items);    
                                 }    
                 }    
                 return $articles;    
 }    
 require_once(&#39;plugin.php&#39;);    
 $menu = computeMenu();    
 $articles    = computeArticles();    
 print_r($menu);    
 print_r($articles);
 //plugin.php 代码如下    
 <?php    
 class MycoolPugin implements Iplugin {    
                 public static function getName(){    
                                 return &#39;MycoolPlugin&#39;;    
                 }    
                 public static function getMenuItems(){    
                                 return array(array(&#39;description&#39;=>&#39;MycoolPlugin&#39;,&#39;link&#39;=>&#39;/MyCoolPlugin&#39;));    
                 }    
                 public static function getArticles(){    
                                 return array(array(&#39;path&#39;=>&#39;/MycoolPlugin&#39;,&#39;title&#39;=>&#39;This is a really cool article&#39;,&#39;text&#39;=>xxxxxxxxx));    
                 }    
 }

The above is the detailed content of Detailed explanation of PHP reflection mechanism and plug-in architecture examples. For more information, please follow other related articles on the PHP Chinese website!

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
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

See all articles

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)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools