Home > Backend Development > PHP Tutorial > PHP reflection class ReflectionClass usage analysis, reflectionclass usage_PHP tutorial

PHP reflection class ReflectionClass usage analysis, reflectionclass usage_PHP tutorial

WBOY
Release: 2016-07-12 08:52:53
Original
971 people have browsed it

PHP reflection class ReflectionClass usage analysis, reflectionclass usage

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

Let’s look at a piece of code first:

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

<&#63;php
class MycoolPugin implements Iplugin
{
 public static
 function getName()
 {
  return 'MycoolPlugin';
 }
 public static
 function getMenuItems()
 {
  return array(array('description' => 'MycoolPlugin', 'link' => '/MyCoolPlugin'));
 }
 public static
 function getArticles()
 {
  return array(array('path' => '/MycoolPlugin', 'title' => 'This is a really cool article', 'text' => xxxxxxxxx));
 }
}

Copy after login

The above code is an application of PHP reflection class.

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 '我是一个类';
 }
}
$class=new ReflectionClass('fuc'); //建立 fuc这个类的反射类

Copy after login

As for what is in the reflection class $class, you can check the manual, but I won’t explain it in detail here

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

Copy after login

There are also some more advanced uses

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

Copy after login

The above process should be familiar. In fact, it is similar to the method of calling an object

It’s just that it’s the other way around, with the method in front and the object in the back

Supplementary : Here we recommend an online PHP code formatting tool on this website, which can facilitate readers to read the compressed PHP code formatted online, which is convenient and practical!

php code online formatting and beautification tool:
http://tools.jb51.net/code/phpformat

Readers who are interested in more PHP-related content can check out the special topics on this site: "Complete PHP Array Operation Skills", "Summary of PHP Sorting Algorithms", "Summary of PHP Common Traversal Algorithms and Techniques", "PHP Data Structure and Algorithm Tutorial", "php Programming Algorithm Summary", "PHP Mathematical Operation Skills Summary", "php Regular Expression Usage Summary", "PHP Operations and Operator Usage Summary", "php String Usage Summary" 》and《Summary of common database operation skills in php》

I hope this article will be helpful to everyone in PHP programming.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1125867.htmlTechArticlephp reflection class ReflectionClass usage analysis, reflectionclass usage This article describes the usage of php reflection class ReflectionClass. Share it with everyone for your reference, the details are as follows: Let’s take a look first...
Related labels:
php
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