Home >Backend Development >PHP Tutorial >PHP Reflection API Example Sharing

PHP Reflection API Example Sharing

高洛峰
高洛峰Original
2016-12-20 16:32:49986browse

The example in this article shares with you the PHP reflection API-the plug-in system architecture implemented using reflection technology for your reference. The specific content is as follows

<?php
/**
 * @name  PHP反射API--利用反射技术实现的插件系统架构
 */
 
 
/**
 * 先调用findPlugins方法获取到获取到实现了接口的类
 * 然后调用反射类的方法
 * @param $method  方法名
 * @param $interfaceName  接口名
 * @return array  方法名对应的返回结果
 */
function compute($method,$interfaceName){
  $findPlugins = findPlugins($interfaceName);
  $menu = array();
  foreach ($findPlugins as $plugin){   //这里获取到实现Iplugin接口的所有的类
    if ($plugin->hasMethod($method)) {    //检查在类中特定的方法是否被定义。
      $reflectionMethod = $plugin->getMethod($method);   //获取类中的方法
      if ($reflectionMethod->isStatic()) {    //判断其方法是否为静态方法
        $items = $reflectionMethod->invoke(null);
      } else {
        $pluginInstance = $plugin->newInstance();  //创建类的新实例。给定参数传递给类构造函数。
        $items = $reflectionMethod->invoke($pluginInstance);
      }
      $menu = array_merge($menu, is_array($items)?$items:[$items]);
    }
  }
  return $menu;
}
 
/**
 * 首先从一堆已定义的类中找到实现Iplugin接口的类
 * 然后将其存放在数组中 return
 * @param string      $interfaceName
 * @return array      $plugins
 */
function findPlugins($interfaceName){
  $plugins = array();
  //返回由已定义类的名字所组成的数组
  foreach (get_declared_classes() as $class){
    $reflectionClass = new ReflectionClass($class);//获得class的反射对象,包括私有的属性方法
    if ($reflectionClass->implementsInterface($interfaceName)) {   //检查它是否实现了Iplugin接口
      $plugins[] = $reflectionClass;   //找到需要反射的类
    }
  }
  return $plugins;
}
interface Iplugin{
  public static function getName();  //定义接口和静态方法
}
//实现Iplugin接口
class MycoolPugin implements Iplugin {
  public static function getName(){
    return &#39;MycoolPlugin&#39;;
  }
  public 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;=> &#39;xxxxxxxxx&#39; ));
  }
}
 
$menu = compute(&#39;getMenuItems&#39;,&#39;Iplugin&#39;);
$articles  = compute(&#39;getArticles&#39;,&#39;Iplugin&#39;);
print_r($menu);
echo "<hr>";
print_r($articles);
echo "<hr>";
$name = compute(&#39;getName&#39;,&#39;Iplugin&#39;);
print_r($name);
 
/*
  new class和new ReflectionClass的区别是什么
  1、new $class() 实例化class对象
  2、new ReflectionClass($class) 获得class的反射对象(包含了元数据信息)
  区别:
  1、new出来的class,你不能访问他的私有属性/方法,但反射可以。
  2、反射返回的对象是class的元数据对象(包含class的所有属性/方法的元数据信息),但不是类本身;类似于查到了类的户口档案,但户口档案不是人!
*/

The above is the entire content of this article. I hope it will be helpful to everyone's learning. I also hope that everyone Duoduo supports PHP Chinese website.

For more PHP reflection API examples and related articles, please pay attention to 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