PHP反射API汇总(转自低调小一博客)
来源:互联网 发布:深度睡眠软件 编辑:程序博客网 时间:2024/06/05 20:53
反射是指在PHP的运行状态中,扩展分析php程序,导出或者提取出关于类、方法、属性、参数等详细信息,甚至包括注释。这种动态获取的信息以及动态调用对象的方法的功能称为反射API。反射是操纵面向对象模型中元模型的API,其功能十分强大,可以帮助我们构建复杂,可扩展的应用。(ps:包括在工厂模式中的使用)
反射API是php内建的oop技术扩展,包括一些类、异常和接口,综合使用他们可用来帮助我们分析其它类,接口,方法,属性和扩展。这些oop扩展被称为反射。
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()
-
- public int 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])
-
- public void setStaticPropertyValue(string name, mixed value)
-
- public array getDefaultProperties()
-
- public bool isIterateable()
- public bool implementsInterface(string name)
-
- public ReflectionExtension getExtension()
- public string getExtensionName()
- }
- ?>
工厂模式应用:
- class MoveDataFactory
- {
-
-
-
-
- public function GetMoveClass($classname)
- {
- $reflectionclass = new ReflectionClass($classname);
- return $reflectionclass->newInstance();
- }
- }
0 0