Home  >  Article  >  Backend Development  >  Usage analysis of php reflection class ReflectionClass_php skills

Usage analysis of php reflection class ReflectionClass_php skills

WBOY
WBOYOriginal
2016-05-16 08:59:523482browse

the example in this article describes the usage of the 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);

plugin.php code is as follows:

<?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));
 }
}

the above code is an application of the 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这个类的反射类

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

there are also some more advanced uses

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

the above process may be familiar. in fact, it is similar to the method of calling the object

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

supplement: here we recommend an online php code formatting tool on this website, which can facilitate readers to format the compressed php code online. reading, 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 topic of this site: "complete php array (array) operation skills ", "php sorting algorithm summary", " summary of commonly used traversal algorithms and techniques in php", "php data structure and algorithm tutorial", "php programming algorithm summary", "php mathematical operation skills summary a>》,《php regular expression usage summary》, "php operations and operator usage summary", "php string (string) usage summary" and "summary of common database operation skills in php"

i hope this article will be helpful to everyone in php programming.

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