Home  >  Article  >  Backend Development  >  Detailed explanation of usage examples of php reflection class ReflectionClass

Detailed explanation of usage examples of php reflection class ReflectionClass

伊谢尔伦
伊谢尔伦Original
2017-07-01 10:57:115604browse

This article mainly introduces the usage of the PHP reflection class ReflectionClass. It analyzes the concept, function and specific usage of the PHP reflection class in detail in the form of examples. Friends in need can refer to the following

Examples of this article The usage of php reflection class ReflectionClass. Share it with everyone for your reference, the details are as follows:

Let’s first look at a piece of code:

/**
 * @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 The code is as follows:

<?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 code is a PHP reflection class application.

What is a PHP reflection class? As the name suggests, it can be understood as a mapping of a class.

For example:

class fuc { //定义一个类
 static
 function ec() {
  echo &#39;我是一个类&#39;;
 }
}
$class=new ReflectionClass(&#39;fuc&#39;); //建立 fuc这个类的反射类

As for what is in the $class reflection class, you can check the manual, which will not be explained in detail here

$fuc=$class->newInstance(); //相当于实例化 fuc 类
$fuc->ec(); //执行 fuc 里的方法ec
/*最后输出:我是一个类*/

There are some more advanced ones. Usage

$ec=$class->getmethod(&#39;ec&#39;); //获取fuc 类中的ec方法
$fuc=$class->newInstance(); //实例化
$ec->invoke($fuc);   //执行ec 方法

The above process should be familiar to you. In fact, it is similar to calling the object's method except that it is reversed here, with the method in front and the object in the back

The above is the detailed content of Detailed explanation of usage examples of php reflection class ReflectionClass. 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