VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > PHP >
  • php反射类ReflectionClass用法分析

这篇文章主要介绍了php反射类ReflectionClass用法,结合实例形式较为详细的分析了php反射类的概念、功能与具体使用方法,需要的朋友可以参考下。

本文实例讲述了php反射类ReflectionClass用法,分享给大家供大家参考,具体如下:

先来看一段代码:

  1. /** 
  2.  * @name PHP反射API--利用反射技术实现的插件系统架构 
  3.  * @author :PHPCQ.COM 
  4.  */ 
  5. interface Iplugin 
  6. { 
  7.  public static 
  8.  function getName(); 
  9. } 
  10. function findPlugins() 
  11. { 
  12.  $plugins = array(); 
  13.  foreach(get_declared_classes() as $class) 
  14.  { 
  15.   $reflectionClass = new ReflectionClass($class); 
  16.   if ($reflectionClass - > implementsInterface('Iplugin')) 
  17.   { 
  18.    $plugins[] = $reflectionClass; 
  19.   } 
  20.  } 
  21.  return $plugins; 
  22. } 
  23. function computeMenu() 
  24. { 
  25.  $menu = array(); 
  26.  foreach(findPlugins() as $plugin) 
  27.  { 
  28.   if ($plugin - > hasMethod('getMenuItems')) 
  29.   { 
  30.    $reflectionMethod = $plugin - > getMethod('getMenuItems'); 
  31.    if ($reflectionMethod - > isStatic()) 
  32.    { 
  33.     $items = $reflectionMethod - > invoke(null); 
  34.    } 
  35.    else 
  36.    { 
  37.     $pluginInstance = $plugin - > newInstance(); 
  38.     $items = $reflectionMethod - > invoke($pluginInstance); 
  39.    } 
  40.    $menu = array_merge($menu, $items); 
  41.   } 
  42.  } 
  43.  return $menu; 
  44. } 
  45. function computeArticles() 
  46. { 
  47.  $articles = array(); 
  48.  foreach(findPlugins() as $plugin) 
  49.  { 
  50.   if ($plugin - > hasMethod('getArticles')) 
  51.   { 
  52.    $reflectionMethod = $plugin - > getMethod('getArticles'); 
  53.    if ($reflectionMethod - > isStatic()) 
  54.    { 
  55.     $items = $reflectionMethod - > invoke(null); 
  56.    } 
  57.    else 
  58.    { 
  59.     $pluginInstance = $plugin - > newInstance(); 
  60.     $items = $reflectionMethod - > invoke($pluginInstance); 
  61.    } 
  62.    $articles = array_merge($articles, $items); 
  63.   } 
  64.  } 
  65.  return $articles; 
  66. } 
  67. require_once('plugin.php'); 
  68. $menu = computeMenu(); 
  69. $articles = computeArticles(); 
  70. print_r($menu); 
  71. print_r($articles); 

plugin.php 代码如下:

  1. <?php 
  2. class MycoolPugin implements Iplugin 
  3. { 
  4.  public static 
  5.  function getName() 
  6.  { 
  7.   return 'MycoolPlugin'; 
  8.  } 
  9.  public static 
  10.  function getMenuItems() 
  11.  { 
  12.   return array(array('description' => 'MycoolPlugin', 'link' => '/MyCoolPlugin')); 
  13.  } 
  14.  public static 
  15.  function getArticles() 
  16.  { 
  17.   return array(array('path' => '/MycoolPlugin', 'title' => 'This is a really cool article', 'text' => xxxxxxxxx)); 
  18.  } 
  19. } 

上述代码是php反射类的一个应用。

什么是php反射类,顾名思义,可以理解为一个类的映射。

举个例子:

  1. class fuc { //定义一个类 
  2.  static 
  3.  function ec() { 
  4.   echo '我是一个类'; 
  5.  } 
  6. } 
  7. $class=new ReflectionClass('fuc'); //建立 fuc这个类的反射类 

至于$class 这反射类里有什么东东,可以查看手册,这里不详解了

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

其中还有一些更高级的用法

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

上面的过程很熟悉吧,其实和调用对象的方法类似

只不过这里是反着来的,方法在前,对象在后

 



出处:http://www.phpfensi.com/php/20210803/17508.html


相关教程