Home  >  Article  >  Backend Development  >  Detailed explanation of PHP reflection mechanism and plug-in architecture examples

Detailed explanation of PHP reflection mechanism and plug-in architecture examples

伊谢尔伦
伊谢尔伦Original
2017-07-01 11:39:381433browse

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

②ReflectionException class

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

③ReflectionFunction class

④ReflectionParameter class:

⑤ReflectionClass 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:

⑦ReflectionProperty class:

⑧ReflectionExtension class

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('Iplugin')) {    
                                                 $plugins[] = $reflectionClass;    
                                 }    
                 }    
                  return $plugins;    
  }    
 function computeMenu(){    
                 $menu = array();    
                 foreach (findPlugins() as $plugin){    
                                 if ($plugin->hasMethod('getMenuItems')) {    
                                                 $reflectionMethod = $plugin->getMethod('getMenuItems');    
                                                 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('getArticles')) {    
                                                $reflectionMethod = $plugin->getMethod('getArticles');    
                                                if ($reflectionMethod->isStatic()) {    
                                                                 $items = $reflectionMethod->invoke(null);    
                                                 } else {    
                                                                 $pluginInstance = $plugin->newInstance();    
                                                                 $items = $reflectionMethod->invoke($pluginInstance);    
                                                 }    
                                                 $articles = array_merge($articles,$items);    
                                 }    
                 }    
                 return $articles;    
 }    
 require_once('plugin.php');    
 $menu = computeMenu();    
 $articles    = computeArticles();    
 print_r($menu);    
 print_r($articles);
 //plugin.php 代码如下    
 'MycoolPlugin','link'=>'/MyCoolPlugin'));    
                 }    
                 public static function getArticles(){    
                                 return array(array('path'=>'/MycoolPlugin','title'=>'This is a really cool article','text'=>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