php 插件 代码架构,php反射机制详以及插件架构实例详解

1。用途:

该扩展分析php程序,导出或提取出关于类、方法、属性、参数等的详细信息,包括注释。

Reflection可以说是对php库函数:“Classes/Objects 类/对象函数”的一个扩展。

主要用在通过程序检测现有php程序内部关于类、方法等信息,并做出处理。

2。API概览:

class Reflection { }

interface Reflector { }

class ReflectionException extends Exception { }

class ReflectionFunction implements Reflector { }

class ReflectionParameter implements Reflector { }

class ReflectionMethod extends ReflectionFunction { }

class ReflectionClass implements Reflector { }

class ReflectionObject extends ReflectionClass { }

class ReflectionProperty implements Reflector { }

class ReflectionExtension implements Reflector { }

3。详细说明:(例子详见php手册)

①Reflection类<?php

class Reflection

{

public static mixed export(Reflector r [,bool return])

//导出一个类或方法的详细信息

public static array getModifierNames(int modifiers)

//取得修饰符的名字

}

?>

②ReflectionException类

该类继承标准类,没特殊方法和属性。

③ReflectionFunction类<?php

class ReflectionFunction implements Reflector

{

final private clone()

public object construct(string name)

public string toString()

public static string export()

//导出该函数的详细信息

public string getName()

//取得函数名

public bool isInternal()

//测试是否为系统内部函数

public bool isUserDefined()

//测试是否为用户自定义函数

public string getFileName()

//取得文件名,包括路径名

public int getStartLine()

//取得定义函数的起始行

public int getEndLine()

//取得定义函数的结束行

public string getDocComment()

//取得函数的注释

public array getStaticVariables()

//取得静态变量

public mixed invoke(mixed* args)

//调用该函数,通过参数列表传参数

public mixed invokeArgs(array args)

//调用该函数,通过数组传参数

public bool returnsReference()

//测试该函数是否返回引用

public ReflectionParameter[] getParameters()

//取得该方法所需的参数,返回值为对象数组

public int getNumberOfParameters()

//取得该方法所需的参数个数

public int getNumberOfRequiredParameters()

//取得该方法所需的参数个数

}

?>

④ReflectionParameter类:<?php

class ReflectionParameter implements Reflector

{

final private clone()

public object construct(string name)

public string toString()

public static string export()

//导出该参数的详细信息

public string getName()

//取得参数名

public bool isPassedByReference()

//测试该参数是否通过引用传递参数

public ReflectionClass getClass()

//若该参数为对象,返回该对象的类名

public bool isArray()

//测试该参数是否为数组类型

public bool allowsNull()

//测试该参数是否允许为空

public bool isOptional()

//测试该参数是否为可选的,当有默认参数时可选

public bool isDefaultValueAvailable()

//测试该参数是否为默认参数

public mixed getDefaultValue()

//取得该参数的默认值

}

?>

⑤ReflectionClass类:<?php

class ReflectionClass implements Reflector

{

final private clone()

public object construct(string name)

public string toString()

public static string export()

//导出该类的详细信息

public string getName()

//取得类名或接口名

public bool isInternal()

//测试该类是否为系统内部类

public bool isUserDefined()

//测试该类是否为用户自定义类

public bool isInstantiable()

//测试该类是否被实例化过

public bool hasConstant(string name)

//测试该类是否有特定的常量

public bool hasMethod(string name)

//测试该类是否有特定的方法

public bool hasProperty(string name)

//测试该类是否有特定的属性

public string getFileName()

//取得定义该类的文件名,包括路径名

public int getStartLine()

//取得定义该类的开始行

public int getEndLine()

//取得定义该类的结束行

public string getDocComment()

//取得该类的注释

public ReflectionMethod getConstructor()

//取得该类的构造函数信息

public ReflectionMethod getMethod(string name)

//取得该类的某个特定的方法信息

public ReflectionMethod[] getMethods()

//取得该类的所有的方法信息

public ReflectionProperty getProperty(string name)

//取得某个特定的属性信息

public ReflectionProperty[] getProperties()

//取得该类的所有属性信息

public array getConstants()

//取得该类所有常量信息

public mixed getConstant(string name)

//取得该类特定常量信息

public ReflectionClass[] getInterfaces()

//取得接口类信息

public bool isInterface()

//测试该类是否为接口

public bool isAbstract()

//测试该类是否为抽象类

public bool isFinal()

//测试该类是否声明为final

public int getModifiers()

//取得该类的修饰符,返回值类型可能是个资源类型

//通过Reflection::getModifierNames($class->getModifiers())进一步读取

public bool isInstance(stdclass object)

//测试传入的对象是否为该类的一个实例

public stdclass newInstance(mixed* args)

//创建该类实例

public ReflectionClass getParentClass()

//取得父类

public bool isSubclassOf(ReflectionClass class)

//测试传入的类是否为该类的父类

public array getStaticProperties()

//取得该类的所有静态属性

public mixed getStaticPropertyValue(string name [, mixed default])

//取得该类的静态属性值,若private,则不可访问

public void setStaticPropertyValue(string name, mixed value)

//设置该类的静态属性值,若private,则不可访问,有悖封装原则

public array getDefaultProperties()

//取得该类的属性信息,不含静态属性

public bool isIterateable()

public bool implementsInterface(string name)

//测试是否实现了某个特定接口

public ReflectionExtension getExtension()

public string getExtensionName()

}

?>

⑥ReflectionMethod类:<?php

class ReflectionMethod extends ReflectionFunction

{

public construct(mixed class, string name)

public string toString()

public static string export()

//导出该方法的信息

public mixed invoke(stdclass object, mixed* args)

//调用该方法

public mixed invokeArgs(stdclass object, array args)

//调用该方法,传多参数

public bool isFinal()

//测试该方法是否为final

public bool isAbstract()

//测试该方法是否为abstract

public bool isPublic()

//测试该方法是否为public

public bool isPrivate()

//测试该方法是否为private

public bool isProtected()

//测试该方法是否为protected

public bool isStatic()

//测试该方法是否为static

public bool isConstructor()

//测试该方法是否为构造函数

public bool isDestructor()

//测试该方法是否为析构函数

public int getModifiers()

//取得该方法的修饰符

public ReflectionClass getDeclaringClass()

//取得该方法所属的类

// Inherited from ReflectionFunction

final private clone()

public string getName()

public bool isInternal()

public bool isUserDefined()

public string getFileName()

public int getStartLine()

public int getEndLine()

public string getDocComment()

public array getStaticVariables()

public bool returnsReference()

public ReflectionParameter[] getParameters()

public int getNumberOfParameters()

public int getNumberOfRequiredParameters()

}

?>

⑦ReflectionProperty类:<?php

class ReflectionProperty implements Reflector

{

final private clone()

public construct(mixed class, string name)

public string toString()

public static string export()

//导出该属性的详细信息

public string getName()

//取得该属性名

public bool isPublic()

//测试该属性名是否为public

public bool isPrivate()

//测试该属性名是否为private

public bool isProtected()

//测试该属性名是否为protected

public bool isStatic()

//测试该属性名是否为static

public bool isDefault()

public int getModifiers()

//取得修饰符

public mixed getValue(stdclass object)

//取得该属性值

public void setValue(stdclass object, mixed value)

//设置该属性值

public ReflectionClass getDeclaringClass()

//取得定义该属性的类

public string getDocComment()

//取得该属性的注释

}

?>

⑧ReflectionExtension类<?php

class ReflectionExtension implements Reflector {

final private clone()

public construct(string name)

public string toString()

public static string export()

//导出该扩展的所有信息

public string getName()

//取得该扩展的名字

public string getVersion()

//取得该扩展的版本

public ReflectionFunction[] getFunctions()

//取得该扩展的所有函数

public array getConstants()

//取得该扩展的所有常量

public array getINIEntries()

//取得与该扩展相关的,在php.ini中的指令信息

public ReflectionClass[] getClasses()

public array getClassNames()

}

?>

4。附:

其实从第二点API概览可以看出:接口挺好用的。

一方面Reflector接口为Reflection小系统提供了一个很好的接口命名规范,

每个实现他的类都须按他的规范,从外部看来,这个小系统API很统一。

另一方面由于很多类实现了Reflector接口,

在这样的类层次结构中,若想实现多态是很容易的。

PHP反射API--利用反射技术实现的插件系统架构/**

* @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 代码如下

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

}

}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441908.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【HDU - 1540】 Tunnel Warfare (线段树进阶操作 区间合并+ 单点更新+ 最长覆盖区间查询 )

题干&#xff1a; During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Generally speaking, villages connected by tunnels lay in a line. Except the two at the ends, every village was …

php扩展包安装了为啥没加载,已安装PHP扩展但未加载

我正在尝试安装php的ssh2扩展,并且有一点点困难.文件在那里,它只是没有加载到PHP.首先,我安装了ssh2&#xff1a;aptitude install libssh2-1-dev libssh2-php(对于它的价值,我在Nginx上运行Ubuntu 12.04.)我可以看到使用modules命令加载ssh2&#xff1a;php -m |grep ssh2ssh2…

【HDU - 1166】敌兵布阵 (线段树模板 单点更新+ 区间查询)

题干&#xff1a; C国的死对头A国这段时间正在进行军事演习&#xff0c;所以C国间谍头子Derek和他手下Tidy又开始忙乎了。A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况。由于采取了某种先进的监测手段&#xff0c;所以每个工兵营地…

php中获取本月第二天,php第二天

//heredoc方式,可以保存长文本 <<<$str <<长文本EOTS;header("Content-Type:text/html;charsetutf-8"); //防止乱码0 1 2 3 4 5 十进制0 1 10 11 100 101 二进制十进制专二进制除二取余二进制专十进制1011*2*20*2*1(1*2/2)5;120 0 1 0 16 3 211111$a…

【HDU - 1754】I Hate It (线段树模板 单点覆盖更新+区间最大值查询)

题干&#xff1a; 很多学校流行一种比较的习惯。老师们很喜欢询问&#xff0c;从某某到某某当中&#xff0c;分数最高的是多少。 这让很多学生很反感。 不管你喜不喜欢&#xff0c;现在需要你做的是&#xff0c;就是按照老师的要求&#xff0c;写一个程序&#xff0c;模拟老…

php微信上传头像,微信小程序怎么上传头像

这次给大家带来的是微信小程序 上传头像的实例详解&#xff0c;最近在做微信小程序上传头像和上传照片功能就随手写一下代码&#xff0c;这篇文章就给大家好好分析一下。上传头像html&#xff1a;头像JS代码// 切换头像changeAvatar: function () {var that this;// var child…

【HDU - 1698】 Just a Hook(线段树模板 区间覆盖更新(laz标记) + 区间和查询 )

题干&#xff1a; In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length. Now Pudge wants to do some operations on the hoo…

反序列化 php R类型,pikachu-PHP反序列化、XXE、SSFR

一、PHP反序列化1.1概述在理解这个漏洞前,你需要先搞清楚php中serialize()&#xff0c;unserialize()这两个函数。序列化serialize()序列化说通俗点就是把一个对象变成可以传输的字符串,比如下面是一个对象:class S{public $test"pikachu";}$snew S(); //创建一个对象…

【POJ - 3468 】 A Simple Problem with Integers (线段树模板 区间更新 + 区间和查询)(不能树状数组或差分数组)

题干&#xff1a; You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval. I…

php向下滑动,js如何判断鼠标滚轮是向下还是向上滚动

判断鼠标滚轮是向上或向下滚动&#xff0c;不同的浏览器的判别方式是不一样的&#xff0c;当前比较流行的浏览器有 IE&#xff0c;Opera&#xff0c;Safari&#xff0c;Firefox&#xff0c;Chrome&#xff0c;在这个问题上Firefox和其他浏览器的实现方式是不一样的。现在通过一…

学习php技巧,对初学者非常有用的PHP技巧

对初学者非常有用的PHP技巧131415161718function add_to_cart($item_id , $qty){if(!is_array($item_id)){$_SESSION[cart][$item_id] $qty;}else{foreach($item_id as $i_id > $qty){$_SESSION[cart][$i_id] $qty;}}}add_to_cart( IPHONE3 , 2 );add_to_cart( array(IPHO…

【51nod - 1065】 最小正子段和( 前缀和排序 )

题干&#xff1a; N个整数组成的序列a11,a22,a33,…,ann&#xff0c;从中选出一个子序列&#xff08;aii,ai1i1,…ajj&#xff09;&#xff0c;使这个子序列的和>0&#xff0c;并且这个和是所有和>0的子序列中最小的。 例如&#xff1a;4&#xff0c;-1&#xff0c;5&a…

oracle trace 文件名,限制oracle trace 文件大小

限制oracle trace 文件大小2007-10-10 17:34:55| 分类&#xff1a; oracle | 标签&#xff1a;|字号max_dump_file_size参数在oracle数据库中用来限制 oracle trace 文件大小max_dump_file_size 的单位是 操作系统块 默认值为 UNLIMITED在有些时候&#xff0c;oracle会产生…

【51nod - 前缀异或】 对前缀和的理解

题干&#xff1a; 前缀异或 基准时间限制&#xff1a;2 秒 空间限制&#xff1a;131072 KB 分值: 5 输入一个长度为n(1 < n < 100000)数组a[1], a[2], ..., a[n]。 输入一个询问数m(1 < m < 100000)和m组询问&#xff0c;每组询问形如(l, r) 对于每组询问(l, …

【C语言实现反转数组】(用栈实现)51nod - 训练营

题干&#xff1a; 输入一个长度为n(1 < n < 100000)数组&#xff0c;倒序输出他。 数组中的元素ai满足(1 < ai < 100000)。 Input 第一行一个整数n&#xff0c;表示数字长度 接下来n行&#xff0c;每行一个整数ai&#xff0c;表示数组的内容。 Output 输出第…

oracle 依赖包自动安装包,ORACLE 安装提示缺少依赖包

安装ORALCE 在做检验时提示缺少依赖包&#xff0c;其实是因为系统是64位系统而这些包都是32位的&#xff0c;This is a prerequisite condition to test whether the package "libaio-0.3.105" is available on the systemThis is a prerequisite condition to test…

oracle中创建实体,生成实体-SqlSugar 4.x-文档园

注意&#xff1a;使用DbFirst数据库账户要有系统表的权限,否则无法读取表的结构1.将库里面所有表都生成实体类文件db.DbFirst.CreateClassFile("c:\\Demo\\1",命名空间);2.指定名表生成 &#xff0c;可以传数组db.DbFirst.Where("Student").CreateClassFil…

永远年轻,永远热泪盈眶----致所有奋斗的ACMer

转载一个弱校ACMer的生涯回忆录&#xff1a;&#xff08;励志一下&#xff0c;顺便给自己一碗鸡汤~&#xff09; 有些事情&#xff0c;承诺了便要将它实现&#xff0c;就像我现在写下这些文字。或许之前我并不该定下“每个退役队员写一篇退役贴”这样一个规矩&#xff0c;这多少…

oracle pl/sql 面试,Oracle SQL 面试题(整理)

1、关于group by表内容&#xff1a;2005-05-09 胜2005-05-09 胜2005-05-09 负2005-05-09 负2005-05-10 胜2005-05-10 负2005-05-10 负如果要生成下列结果, 该如何写sql语句?胜 负2005-05-09 2 22005-05-10 1 2创建过程如下&#xff1a;create table tmp(rq varchar(10),shengf…

【HDU - 5094】 Maze (状态压缩+bfs)

题干&#xff1a; This story happened on the background of Star Trek. Spock, the deputy captain of Starship Enterprise, fell into Klingon’s trick and was held as prisoner on their mother planet Qo’noS. The captain of Enterprise, James T. Kirk, had to f…