This article mainly introduces the detailed explanation of PHP Reflection API. This article explains the Reflection class, ReflectionException class, ReflectionFunction class, ReflectionParameter class, ReflectionClass class, ReflectionMethod class. For more information, friends who need it can refer to it
PHP Reflection API is a new feature only available in PHP5. It is used to export or extract detailed information about classes, methods, properties, parameters, etc., including comments.
PHP Reflection API includes:
?
2 3 4
|
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 { } |
1 2 3 4 5 6 7 8 9 | <🎜>class Reflection<🎜> <🎜>{<🎜> <🎜>public static mixed export(Reflector r [,bool return])<🎜> <🎜>//Export detailed information of a class or method<🎜> <🎜>public static array getModifierNames(int modifiers)<🎜> <🎜>//Get the name of the modifier<🎜> <🎜>}<🎜> <🎜>?> |
②ReflectionException class
This class inherits the standard class and has no special methods and attributes.
③ReflectionFunction class
?
|
<🎜>class ReflectionFunction implements Reflector<🎜> <🎜>{<🎜> <🎜>final private __clone()<🎜> <🎜>public object __construct(string name)<🎜> <🎜>public string __toString()<🎜> <🎜>public static string export()<🎜> <🎜>//Export the detailed information of this function<🎜> <🎜>public string getName()<🎜> <🎜>//Get function name<🎜> <🎜>public bool isInternal()<🎜> <🎜>//Test whether it is an internal function of the system<🎜> <🎜>public bool isUserDefined()<🎜> <🎜>//Test whether it is a user-defined function<🎜> <🎜>public string getFileName()<🎜> <🎜>//Get the file name, including path name<🎜> <🎜>public int getStartLine()<🎜> <🎜>//Get the starting line of the defined function<🎜> <🎜>public int getEndLine()<🎜> <🎜>//Get the end line of the defined function<🎜> <🎜>public string getDocComment()<🎜> <🎜>//Get the comment of the function<🎜> <🎜>public array getStaticVariables()<🎜> <🎜>//Get static variables<🎜> <🎜>public mixed invoke(mixed* args)<🎜> <🎜>//Call this function and pass parameters through the parameter list<🎜> <🎜>public mixed invokeArgs(array args)<🎜> <🎜>//Call this function and pass parameters through the array<🎜> <🎜>public bool returnsReference()<🎜> <🎜>//Test whether the function returns a reference<🎜> <🎜>public ReflectionParameter[] getParameters()<🎜> <🎜>//Get the parameters required by this method, and the return value is an object array<🎜> <🎜>public int getNumberOfParameters()<🎜> <🎜>//Get the number of parameters required by this method<🎜> <🎜>public int getNumberOfRequiredParameters()<🎜> <🎜>//Get the number of parameters required by this method<🎜> <🎜>}<🎜> <🎜>?> |
④ReflectionParameter类:
?
|
<🎜>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 class:
?
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 72 73 74 75 76 77 78 |
class ReflectionClass implements Reflector { final private __clone() public object __construct(string name) public string __toString() public static string export() //Export detailed information of this class public string getName() //Get the class name or interface name public bool isInternal() //Test whether this class is a system internal class public bool isUserDefined() //Test whether the class is a user-defined class public bool isInstantiable() //Test whether the class has been instantiated public bool hasConstant(string name) //Test whether the class has specific constants public bool hasMethod(string name) //Test whether the class has a specific method public bool hasProperty(string name) //Test whether the class has specific attributes public string getFileName() //Get the file name that defines this class, including the path name public int getStartLine() //Get the starting line that defines this class public int getEndLine() //Get the end line that defines this class public string getDocComment() //Get the comments of this class public ReflectionMethod getConstructor() //Get the constructor information of this class public ReflectionMethod getMethod(string name) //Get a specific method information of this class public ReflectionMethod[] getMethods() //Get all method information of this class public ReflectionProperty getProperty(string name) //Get a specific attribute information public ReflectionProperty[] getProperties() //Get all attribute information of this class public array getConstants() //Get all constant information of this class public mixed getConstant(string name) //Get the specific constant information of this type public ReflectionClass[] getInterfaces() //Get interface class information public bool isInterface() //Test whether the class is an interface public bool isAbstract() //Test whether the class is an abstract class public bool isFinal() //Test whether the class is declared final public int getModifiers() //Get the modifier of this class, the return value type may be a resource type //Further reading through Reflection::getModifierNames($class->getModifiers()) public bool isInstance(stdclass object) //Test whether the passed in object is an instance of this class public stdclass newInstance(mixed* args) //Create an instance of this class public ReflectionClass getParentClass() //Get the parent class public bool isSubclassOf(ReflectionClass class) //Test whether the passed in class is the parent class of this class public array getStaticProperties() //Get all static properties of this class public mixed getStaticPropertyValue(string name [, mixed default]) //Get the static attribute value of this class. If it is private, it is inaccessible public void setStaticPropertyValue(string name, mixed value) //Set the static attribute value of this class. If it is private, it is inaccessible and violates the principle of encapsulation public array getDefaultProperties() //Get the attribute information of this class, excluding static attributes public bool isIterateable() public bool implementsInterface(string name) //Test whether a specific interface is implemented public ReflectionExtension getExtension() public string getExtensionName() } ?> |
⑥ReflectionMethod class:
?
|
<🎜>class ReflectionMethod extends ReflectionFunction<🎜>
<🎜>{<🎜>
<🎜>public __construct(mixed class, string name)<🎜>
<🎜>public string __toString()<🎜>
<🎜>public static string export()<🎜>
<🎜>//Export information about this method<🎜>
<🎜>public mixed invoke(stdclass object, mixed* args)<🎜>
<🎜>//Call this method<🎜>
<🎜>public mixed invokeArgs(stdclass object, array args)<🎜>
<🎜>//Call this method and pass multiple parameters<🎜>
<🎜>public bool isFinal()<🎜>
<🎜>//Test whether the method is final<🎜>
<🎜>public bool isAbstract()<🎜>
<🎜>//Test whether the method is abstract<🎜>
<🎜>public bool isPublic()<🎜>
<🎜>//Test whether the method is public<🎜>
<🎜>public bool isPrivate()<🎜>
<🎜>//Test whether the method is private<🎜>
<🎜>public bool isProtected()<🎜>
<🎜>//Test whether the method is protected<🎜>
<🎜>public bool isStatic()<🎜>
<🎜>//Test whether the method is static<🎜>
<🎜>public bool isConstructor()<🎜>
<🎜>//Test whether the method is a constructor<🎜>
<🎜>public bool isDestructor()<🎜>
<🎜>//Test whether the method is a destructor<🎜>
<🎜>public int getModifiers()<🎜>
<🎜>//Get the modifier of this method<🎜>
<🎜>public ReflectionClass getDeclaringClass()<🎜>
<🎜>//Get the class to which this method belongs<🎜>
<🎜>// 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类: ?
Usage example: ?
|