gpt4 book ai didi

php反射类ReflectionClass用法分析

转载 作者:qq735679552 更新时间:2022-09-28 22:32:09 25 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章php反射类ReflectionClass用法分析由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

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

先来看一段代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
  * @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 代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?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));
  }
}

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

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

举个例子:

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

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

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

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

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

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

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

希望本文所述对大家PHP程序设计有所帮助.

最后此篇关于php反射类ReflectionClass用法分析的文章就讲到这里了,如果你想了解更多关于php反射类ReflectionClass用法分析的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com