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

Detailed explanation of usage examples of php reflection class ReflectionClass

伊谢尔伦
Release: 2023-03-12 09:28:02
Original
5637 people have browsed it

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);
Copy after login

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));
 }
}
Copy after login

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这个类的反射类
Copy after login

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
/*最后输出:我是一个类*/
Copy after login

There are some more advanced ones. Usage

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template