合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
# ReflectionClass 类报告了一个类的有关信息。 > 报告了一个类的有关信息 ``` <pre class="calibre10">``` ReflectionClass implements <span class="token4">Reflector</span> <span class="token3">{</span> <span class="token">/* 常量 */</span> const integer IS_IMPLICIT_ABSTRACT <span class="token1">=</span> <span class="token6">16</span><span class="token3">;</span><span class="token">//指示了类是一个抽象类(abstract), 因为它有抽象(abstract)方法</span> const integer IS_EXPLICIT_ABSTRACT <span class="token1">=</span> <span class="token6">32</span> <span class="token3">;</span><span class="token">//指示了类是一个抽象类(abstract), 因为它已明确定义。</span> const integer IS_FINAL <span class="token1">=</span> <span class="token6">64</span> <span class="token3">;</span><span class="token">//指示这是一个 final 类.//类的名称。只读,并在尝试赋值的时候会抛出 ReflectionException</span> <span class="token">/* 属性 */</span> public $name <span class="token3">;</span> <span class="token">/* 方法 */</span> public __construct <span class="token3">(</span> mixed $argument <span class="token3">)</span> <span class="token">//初始化 ReflectionClass 类</span> public static export <span class="token3">(</span> mixed $argument <span class="token3">[</span><span class="token3">,</span> bool $<span class="token5">return</span> <span class="token1">=</span> <span class="token6">false</span> <span class="token3">]</span> <span class="token3">)</span> <span class="token3">:</span> string <span class="token">//导出一个类</span> public getConstant <span class="token3">(</span> string $name <span class="token3">)</span> <span class="token3">:</span> mixed <span class="token">//获取定义过的一个常量</span> public getConstants <span class="token3">(</span> void <span class="token3">)</span> <span class="token3">:</span> array<span class="token">//获取一组常量</span> public getConstructor <span class="token3">(</span> void <span class="token3">)</span> <span class="token3">:</span> ReflectionMethod<span class="token">//获取类的构造函数</span> public getDefaultProperties <span class="token3">(</span> void <span class="token3">)</span> <span class="token3">:</span> array <span class="token">//获取默认属性</span> public getDocComment <span class="token3">(</span> void <span class="token3">)</span> <span class="token3">:</span> string <span class="token">//获取文档注释</span> public getEndLine <span class="token3">(</span> void <span class="token3">)</span> <span class="token3">:</span> int <span class="token">//获取最后一行的行数</span> public getExtension <span class="token3">(</span> void <span class="token3">)</span> <span class="token3">:</span> ReflectionExtension <span class="token">//根据已定义的类获取所在扩展的 ReflectionExtension 对象</span> public getExtensionName <span class="token3">(</span> void <span class="token3">)</span> <span class="token3">:</span> string <span class="token">//获取定义的类所在的扩展的名称</span> public getFileName <span class="token3">(</span> void <span class="token3">)</span> <span class="token3">:</span> string <span class="token">//获取定义类的文件名</span> public getInterfaceNames <span class="token3">(</span> void <span class="token3">)</span> <span class="token3">:</span> array <span class="token">//获取接口(interface)名称</span> public getInterfaces <span class="token3">(</span> void <span class="token3">)</span> <span class="token3">:</span> array <span class="token">//获取接口</span> public getMethod <span class="token3">(</span> string $name <span class="token3">)</span> <span class="token3">:</span> ReflectionMethod <span class="token">//获取一个类方法的 ReflectionMethod</span> public getMethods <span class="token3">(</span><span class="token3">[</span> int $filter <span class="token3">]</span> <span class="token3">)</span> <span class="token3">:</span> array <span class="token">//获取所有方法组成的数组</span> public getModifiers <span class="token3">(</span> void <span class="token3">)</span> <span class="token3">:</span> int <span class="token">//获取类的修饰符</span> public getName <span class="token3">(</span> void <span class="token3">)</span> <span class="token3">:</span> string <span class="token">//获取类名</span> public getNamespaceName <span class="token3">(</span> void <span class="token3">)</span> <span class="token3">:</span> string <span class="token">//获取命名空间的名称</span> public getParentClass <span class="token3">(</span> void <span class="token3">)</span> <span class="token3">:</span> ReflectionClass <span class="token">//获取父类</span> public getProperties <span class="token3">(</span><span class="token3">[</span> int $filter <span class="token3">]</span> <span class="token3">)</span> <span class="token3">:</span> array <span class="token">//获取一组属性 </span> public getProperty <span class="token3">(</span> string $name <span class="token3">)</span> <span class="token3">:</span> ReflectionProperty <span class="token">//获取类的一个属性的 ReflectionProperty</span> public getReflectionConstant <span class="token3">(</span> string $name <span class="token3">)</span> <span class="token3">:</span> ReflectionClassConstant <span class="token">//获取类的常量的ReflectionClassConstant</span> public getReflectionConstants <span class="token3">(</span> void <span class="token3">)</span> <span class="token3">:</span> array <span class="token">//获取类常量</span> public getShortName <span class="token3">(</span> void <span class="token3">)</span> <span class="token3">:</span> string <span class="token">//获取短名</span> public getStartLine <span class="token3">(</span> void <span class="token3">)</span> <span class="token3">:</span> int <span class="token">//获取起始行号</span> public getStaticProperties <span class="token3">(</span> void <span class="token3">)</span> <span class="token3">:</span> array <span class="token">//获取静态(static)属性</span> public getStaticPropertyValue <span class="token3">(</span> string $name <span class="token3">[</span><span class="token3">,</span> mixed <span class="token1">&</span>$def_value <span class="token3">]</span> <span class="token3">)</span> <span class="token3">:</span> mixed <span class="token">//获取静态(static)属性的值</span> public getTraitAliases <span class="token3">(</span> void <span class="token3">)</span> <span class="token3">:</span> array <span class="token">//返回 trait 别名的一个数组</span> public getTraitNames <span class="token3">(</span> void <span class="token3">)</span> <span class="token3">:</span> array <span class="token">//返回这个类所使用 traits 的名称的数组</span> public getTraits <span class="token3">(</span> void <span class="token3">)</span> <span class="token3">:</span> array <span class="token">//返回这个类所使用的 traits 数组</span> public hasConstant <span class="token3">(</span> string $name <span class="token3">)</span> <span class="token3">:</span> bool <span class="token">//检查常量是否已经定义</span> public hasMethod <span class="token3">(</span> string $name <span class="token3">)</span> <span class="token3">:</span> bool <span class="token">//检查方法是否已定义</span> public hasProperty <span class="token3">(</span> string $name <span class="token3">)</span> <span class="token3">:</span> bool <span class="token">//检查属性是否已定义</span> public implementsInterface <span class="token3">(</span> string $interface <span class="token3">)</span> <span class="token3">:</span> bool <span class="token">//接口的实现</span> public inNamespace <span class="token3">(</span> void <span class="token3">)</span> <span class="token3">:</span> bool <span class="token">//检查是否位于命名空间中</span> public isAbstract <span class="token3">(</span> void <span class="token3">)</span> <span class="token3">:</span> bool <span class="token">//检查类是否是抽象类(abstract)</span> public isAnonymous <span class="token3">(</span> void <span class="token3">)</span> <span class="token3">:</span> bool <span class="token">//检查类是否是匿名类</span> public isCloneable <span class="token3">(</span> void <span class="token3">)</span> <span class="token3">:</span> bool <span class="token">//返回了一个类是否可复制</span> public isFinal <span class="token3">(</span> void <span class="token3">)</span> <span class="token3">:</span> bool <span class="token">//检查类是否声明为 final</span> public isInstance <span class="token3">(</span> object $object <span class="token3">)</span> <span class="token3">:</span> bool <span class="token">//检查类的实例</span> public isInstantiable <span class="token3">(</span> void <span class="token3">)</span> <span class="token3">:</span> bool <span class="token">//检查类是否可实例化</span> public isInterface <span class="token3">(</span> void <span class="token3">)</span> <span class="token3">:</span> bool <span class="token">//检查类是否是一个接口(interface)</span> public isInternal <span class="token3">(</span> void <span class="token3">)</span> <span class="token3">:</span> bool <span class="token">//检查类是否由扩展或核心在内部定义</span> public isIterable <span class="token3">(</span> void <span class="token3">)</span> <span class="token3">:</span> bool <span class="token">//检查此类是否可迭代</span> public isIterateable <span class="token3">(</span> void <span class="token3">)</span> <span class="token3">:</span> bool <span class="token">//检查是否可迭代(iterateable)</span> public isSubclassOf <span class="token3">(</span> string $class <span class="token3">)</span> <span class="token3">:</span> bool <span class="token">//检查是否为一个子类</span> public isTrait <span class="token3">(</span> void <span class="token3">)</span> <span class="token3">:</span> bool <span class="token">//返回了是否为一个 trait</span> public isUserDefined <span class="token3">(</span> void <span class="token3">)</span> <span class="token3">:</span> bool <span class="token">//检查是否由用户定义的</span> public newInstance <span class="token3">(</span> mixed $args <span class="token3">[</span><span class="token3">,</span> mixed $<span class="token3">.</span><span class="token3">.</span><span class="token3">.</span> <span class="token3">]</span> <span class="token3">)</span> <span class="token3">:</span> object <span class="token">//创建类的新的实例。给出的参数将会传递到类的构造函数。接受可变数目的参数,用于传递到类的构造函数,和call_user_func()很相似</span> public newInstanceArgs <span class="token3">(</span><span class="token3">[</span> array $args <span class="token3">]</span> <span class="token3">)</span> <span class="token3">:</span> object <span class="token">//从给出的参数创建一个新的类实例,给出的参数将传递到类的构造函数</span> public newInstanceWithoutConstructor <span class="token3">(</span> void <span class="token3">)</span> <span class="token3">:</span> object <span class="token">//创建一个新的类实例而不调用它的构造函数</span> public setStaticPropertyValue <span class="token3">(</span> string $name <span class="token3">,</span> string $value <span class="token3">)</span> <span class="token3">:</span> void <span class="token">//设置静态属性的值</span> public __toString <span class="token3">(</span> void <span class="token3">)</span> <span class="token3">:</span> strin <span class="token">//返回 ReflectionClass 对象字符串的表示形式</span> <span class="token3">}</span> ``` ``` # **例子:** ## **检查类** ``` <pre class="calibre10">``` <span class="token5">function</span> <span class="token4">classData</span><span class="token3">(</span>ReflectionClass $class<span class="token3">)</span> <span class="token3">{</span> $details <span class="token1">=</span> <span class="token2">''</span><span class="token3">;</span> $name <span class="token1">=</span> $class<span class="token1">-</span><span class="token1">></span><span class="token4">getName</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token">// 返回要检查的类名</span> <span class="token5">if</span> <span class="token3">(</span>$class<span class="token1">-</span><span class="token1">></span><span class="token4">isUserDefined</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span> <span class="token3">{</span> <span class="token">// 检查类是否由用户定义</span> $details <span class="token3">.</span><span class="token1">=</span> <span class="token2">"$name 类是用户自定义的"</span> <span class="token3">.</span> PHP_EOL<span class="token3">;</span> <span class="token3">}</span> <span class="token5">if</span> <span class="token3">(</span>$class<span class="token1">-</span><span class="token1">></span><span class="token4">isInternal</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span> <span class="token3">{</span> <span class="token">// 检查类是否由扩展或核心在内部定义</span> $details <span class="token3">.</span><span class="token1">=</span> <span class="token2">"$name is built-in"</span> <span class="token3">.</span> PHP_EOL<span class="token3">;</span> <span class="token3">}</span> <span class="token5">if</span> <span class="token3">(</span>$class<span class="token1">-</span><span class="token1">></span><span class="token4">isInterface</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span> <span class="token3">{</span> <span class="token">// 检查类是否是一个接口</span> $details <span class="token3">.</span><span class="token1">=</span> <span class="token2">"$name 是一个接口类"</span> <span class="token3">.</span> PHP_EOL<span class="token3">;</span> <span class="token3">}</span> <span class="token5">if</span> <span class="token3">(</span>$class<span class="token1">-</span><span class="token1">></span><span class="token4">isAbstract</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span> <span class="token3">{</span> <span class="token">// 检查类是否是抽象类</span> $details <span class="token3">.</span><span class="token1">=</span> <span class="token2">"$name 是一个抽象类"</span> <span class="token3">.</span> PHP_EOL<span class="token3">;</span> <span class="token3">}</span> <span class="token5">if</span> <span class="token3">(</span>$class<span class="token1">-</span><span class="token1">></span><span class="token4">isFinal</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span> <span class="token3">{</span> <span class="token">// 检查类是否声明为 final</span> $details <span class="token3">.</span><span class="token1">=</span> <span class="token2">"$name 是一个final类"</span> <span class="token3">.</span> PHP_EOL<span class="token3">;</span> <span class="token3">}</span> <span class="token5">if</span> <span class="token3">(</span>$class<span class="token1">-</span><span class="token1">></span><span class="token4">isInstantiable</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span> <span class="token3">{</span> <span class="token">// 检查类是否可实例化</span> $details <span class="token3">.</span><span class="token1">=</span> <span class="token2">"$name 类可以被实例化"</span> <span class="token3">.</span> PHP_EOL<span class="token3">;</span> <span class="token3">}</span> <span class="token5">else</span> <span class="token3">{</span> $details <span class="token3">.</span><span class="token1">=</span> <span class="token2">"$name 类不能实例化"</span> <span class="token3">.</span> PHP_EOL<span class="token3">;</span> <span class="token3">}</span> <span class="token5">return</span> $details<span class="token3">;</span> <span class="token3">}</span> $prodClass <span class="token1">=</span> <span class="token5">new</span> <span class="token4">ReflectionClass</span><span class="token3">(</span><span class="token2">'User'</span><span class="token3">)</span><span class="token3">;</span> print <span class="token4">classData</span><span class="token3">(</span>$prodClass<span class="token3">)</span><span class="token3">;</span> ``` ``` ## **获取内容信息** ``` <pre class="calibre10">``` <span class="token5">function</span> <span class="token4">getClassSource</span><span class="token3">(</span>ReflectionClass $class<span class="token3">)</span> <span class="token3">{</span> $path <span class="token1">=</span> $class<span class="token1">-</span><span class="token1">></span><span class="token4">getFileName</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token">// 获取类文件的绝对路径</span> $lines <span class="token1">=</span> @<span class="token4">file</span><span class="token3">(</span>$path<span class="token3">)</span><span class="token3">;</span> <span class="token">// 由路径获得由文件中所有行组成的数组(包括User以外的整个文件内容)</span> $from <span class="token1">=</span> $class<span class="token1">-</span><span class="token1">></span><span class="token4">getStartLine</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token">// 提供类的起始行</span> $to <span class="token1">=</span> $class<span class="token1">-</span><span class="token1">></span><span class="token4">getEndLine</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token">// 提供类的终止行</span> $len <span class="token1">=</span> $to <span class="token1">-</span> $from <span class="token1">+</span> <span class="token6">1</span><span class="token3">;</span> <span class="token5">return</span> <span class="token4">implode</span><span class="token3">(</span><span class="token4">array_slice</span><span class="token3">(</span>$lines<span class="token3">,</span> $from <span class="token1">-</span> <span class="token6">1</span><span class="token3">,</span> $len<span class="token3">)</span><span class="token3">)</span><span class="token3">;</span><span class="token">//提取User类部分并将此数组拼接为字符串</span> <span class="token3">}</span> $prodClass <span class="token1">=</span> <span class="token5">new</span> <span class="token4">ReflectionClass</span><span class="token3">(</span><span class="token2">'User'</span><span class="token3">)</span><span class="token3">;</span> <span class="token4">var_dump</span><span class="token3">(</span><span class="token4">getClassSource</span><span class="token3">(</span>$prodClass<span class="token3">)</span><span class="token3">)</span><span class="token3">;</span> ``` ``` ## **\# 检查方法** > - 获得 ReflectionFunctionAbstract 下的ReflectionMethod 对象的方法有两种:第一种是通过 ReflectionClass::getMethods() 获得 ReflectionMethod 对象的数组,会返回类中所有方法的 ReflectionMethod 对象。无需知道方法名 > - 第二种是直接使用 ReflectionMethod 类实例化对象:`new ReflectionMethod('User', 'getName');`,这种方式只能获取一个类方法对象,需要提前知道方法名。 > ReflectionMethod 对象的方法例子参考下节的ReflectionMethod类 ``` <pre class="calibre10">``` <span class="token">//调用B的show方法时候去调用A的show方法</span> class <span class="token4">A</span><span class="token3">{</span> <span class="token5">function</span> <span class="token4">show</span><span class="token3">(</span>$param<span class="token1">=</span><span class="token6">20</span><span class="token3">)</span><span class="token3">{</span> echo <span class="token2">"classA的show方法,参数值为{$param}"</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> class <span class="token4">B</span><span class="token3">{</span> private $obj<span class="token3">;</span> <span class="token5">function</span> <span class="token4">__construct</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> $this<span class="token1">-</span><span class="token1">></span>obj <span class="token1">=</span> <span class="token5">new</span> <span class="token4">A</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token5">function</span> <span class="token4">__call</span><span class="token3">(</span>$name<span class="token3">,</span> $arguments<span class="token3">)</span> <span class="token3">{</span> $ref <span class="token1">=</span> <span class="token5">new</span> <span class="token4">ReflectionClass</span><span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>obj<span class="token3">)</span><span class="token3">;</span> <span class="token">//检查方法是否已定义</span> <span class="token5">if</span> <span class="token3">(</span>$ref<span class="token1">-</span><span class="token1">></span><span class="token4">hasMethod</span><span class="token3">(</span>$name<span class="token3">)</span><span class="token3">)</span><span class="token3">{</span> <span class="token">//获取一个类方法的 ReflectionMethod对象。相当于new ReflectionMethod('class', 'myMethod')</span> $method <span class="token1">=</span> $ref<span class="token1">-</span><span class="token1">></span><span class="token4">getMethod</span><span class="token3">(</span>$name<span class="token3">)</span><span class="token3">;</span> <span class="token5">if</span> <span class="token3">(</span>$method<span class="token1">-</span><span class="token1">></span><span class="token4">isPublic</span><span class="token3">(</span><span class="token3">)</span><span class="token1">&&</span><span class="token1">!</span>$method<span class="token1">-</span><span class="token1">></span><span class="token4">isAbstract</span><span class="token3">(</span><span class="token3">)</span><span class="token1">&&</span><span class="token4">count</span><span class="token3">(</span>$arguments<span class="token3">)</span><span class="token1">==</span><span class="token6">0</span><span class="token3">)</span><span class="token3">{</span> <span class="token5">if</span> <span class="token3">(</span>$method<span class="token1">-</span><span class="token1">></span><span class="token4">isStatic</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span><span class="token3">{</span> <span class="token">//如果是静态方法,不需要传入调用的对象</span> $method<span class="token1">-</span><span class="token1">></span><span class="token4">invoke</span><span class="token3">(</span><span class="token5">null</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span><span class="token5">else</span><span class="token3">{</span> $method<span class="token1">-</span><span class="token1">></span><span class="token4">invoke</span><span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>obj<span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span><span class="token5">else</span><span class="token3">{</span> <span class="token5">if</span> <span class="token3">(</span>$method<span class="token1">-</span><span class="token1">></span><span class="token4">isStatic</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span><span class="token3">{</span> <span class="token">//如果是静态方法,不需要传入调用的对象</span> $method<span class="token1">-</span><span class="token1">></span><span class="token4">invokeArgs</span><span class="token3">(</span><span class="token5">null</span><span class="token3">,</span>$arguments<span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span><span class="token5">else</span><span class="token3">{</span> $method<span class="token1">-</span><span class="token1">></span><span class="token4">invokeArgs</span><span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>obj<span class="token3">,</span>$arguments<span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token3">}</span> $b<span class="token1">=</span><span class="token5">new</span> <span class="token4">B</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> $b<span class="token1">-</span><span class="token1">></span><span class="token4">show</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span><span class="token">//classA的show方法,参数值为20</span> $b<span class="token1">-</span><span class="token1">></span><span class="token4">show</span><span class="token3">(</span><span class="token6">21</span><span class="token3">)</span><span class="token3">;</span><span class="token">//classA的show方法,参数值为21</span> ``` ``` 插件案例 ``` <pre class="calibre17">``` include_once __DIR__<span class="token3">.</span><span class="token2">"/plugin.php"</span><span class="token3">;</span> <span class="token5">function</span> <span class="token4">get_plugin_menus</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> $menus <span class="token1">=</span> <span class="token4">array</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> $all_class <span class="token1">=</span> <span class="token4">get_declared_classes</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span><span class="token">//获取所有的类</span> foreach <span class="token3">(</span>$all_class as $cls<span class="token3">)</span><span class="token3">{</span> $ref_cls <span class="token1">=</span> <span class="token5">new</span> <span class="token4">ReflectionClass</span><span class="token3">(</span>$cls<span class="token3">)</span><span class="token3">;</span> <span class="token5">if</span> <span class="token3">(</span>$ref_cls<span class="token1">-</span><span class="token1">></span><span class="token4">implementsInterface</span><span class="token3">(</span><span class="token2">'Plugin'</span><span class="token3">)</span><span class="token3">)</span><span class="token3">{</span><span class="token">//是否实现了某个接口</span> <span class="token5">if</span> <span class="token3">(</span>$ref_cls<span class="token1">-</span><span class="token1">></span><span class="token4">hasMethod</span><span class="token3">(</span><span class="token2">'showMenu'</span><span class="token3">)</span><span class="token3">)</span><span class="token3">{</span> $method <span class="token1">=</span> $ref_cls<span class="token1">-</span><span class="token1">></span><span class="token4">getMethod</span><span class="token3">(</span><span class="token2">"showMenu"</span><span class="token3">)</span><span class="token3">;</span> <span class="token5">if</span> <span class="token3">(</span>$method<span class="token1">-</span><span class="token1">></span><span class="token4">isStatic</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span><span class="token3">{</span> $method<span class="token1">-</span><span class="token1">></span><span class="token4">invoke</span><span class="token3">(</span><span class="token5">null</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token5">else</span><span class="token3">{</span> <span class="token">//$method->invoke(new $cls());//这样获取类</span> $instance <span class="token1">=</span> $ref_cls<span class="token1">-</span><span class="token1">></span><span class="token4">newInstance</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> $menu <span class="token1">=</span> $method<span class="token1">-</span><span class="token1">></span><span class="token4">invoke</span><span class="token3">(</span>$instance<span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token3">}</span> $menus <span class="token1">=</span> <span class="token4">array_merge</span><span class="token3">(</span>$menus<span class="token3">,</span>$menu<span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token5">return</span> $menus<span class="token3">;</span> <span class="token3">}</span> echo <span class="token2">""</span><span class="token3">;</span><span class="token4">get_plugin_menus</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>echo <span class="token2">""</span><span class="token3">;</span> interface <span class="token4">Plugin</span><span class="token3">{</span> <span class="token5">function</span> <span class="token4">showMenu</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> class <span class="token4">MyPlugin</span> implements <span class="token4">Plugin</span><span class="token3">{</span> <span class="token5">function</span> <span class="token4">showMenu</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span> $menu <span class="token1">=</span> <span class="token4">array</span><span class="token3">(</span> <span class="token4">array</span><span class="token3">(</span> <span class="token2">'name'</span> <span class="token1">=</span><span class="token1">></span> <span class="token2">'menu1'</span><span class="token3">,</span> <span class="token2">'link'</span> <span class="token1">=</span><span class="token1">></span> <span class="token2">'index.php?act=link1'</span> <span class="token3">)</span><span class="token3">,</span> <span class="token4">array</span><span class="token3">(</span> <span class="token2">'name'</span> <span class="token1">=</span><span class="token1">></span> <span class="token2">'menu2'</span><span class="token3">,</span> <span class="token2">'link'</span> <span class="token1">=</span><span class="token1">></span> <span class="token2">'index.php?act=link2'</span> <span class="token3">)</span> <span class="token3">)</span><span class="token3">;</span> <span class="token5">return</span> $menu<span class="token3">;</span> <span class="token3">}</span> ``` ```