PHP Reflection examples and PHP Reflection APIs

Source: Internet
Author: User
Tags reflector
This article mainly introduces PHP Reflection examples and PHP Reflection API Chinese descriptions. it focuses on the Chinese description of every method in PHP Reflection API, you can refer to the number, name, and order of parameters that need to be obtained in a class method during the development process. The parameter names can be used to GET values from $ _ GET.

For example, if the method prototype is test ($ uid, $ score), you need to GET the result from $ _ GET.

The code is as follows:


$ Uid = $ _ GET ['uid'];

$ Score = $ _ GET ['score '];

Then call the $ obj-> test ($ uid, $ score) method)

Of course, the premise is that the parameter name is the same as the variable name passed by the get method.

Using the PHP Reflection API, you can obtain the function parameter name and default value as follows:

The code is as follows:


<? Php
Class testClass {

Public function testFunc ($ param1, $ param2 = 0 ){

}
}

$ Method = new ReflectionMethod ('testclass', 'testfunc ');
$ Params = $ method ---> getParameters ();
Foreach ($ params as $ param ){
Echo 'param name: '. $ param-> getName (), "\ n ";
If ($ param-> isOptional ()){
Echo 'default value: '. $ param-> getDefaultValue (), "\ n ";
}
}

The following describes the PHP Reflection API:

1. purpose:
This extension analyzes php programs and exports or extracts detailed information about classes, methods, attributes, parameters, and so on, including comments.
Reflection is an extension of the php library function "Classes/Objects class/object function.
It is mainly used to detect and process information about classes and methods in existing php programs through programs.

2. API overview:

The code is as follows:


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. Details: (for example, see The php Manual)

The code is as follows:


① Reflection class
<? Php
Class Reflection
{
Public static mixed export (Reflector r [, bool return])
// Export the details of a class or method
Public static array getModifierNames (int modifiers)
// Get the modifier name
}
?>

② ReflectionException class

This class inherits standard classes without special methods and attributes.

③ ReflectionFunction class
<? Php
Class ReflectionFunction implements Reflector
{
Final private _ clone ()
Public object _ construct (string name)
Public string _ toString ()
Public static string export ()
// Export the detailed information of the function
Public string getName ()
// Obtain the function name
Public bool isInternal ()
// Test whether it is a system internal function
Public bool isUserDefined ()
// Test whether the function is a user-defined function
Public string getFileName ()
// Get the file name, including the 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 ()
// Obtain function comments
Public array getStaticVariables ()
// Obtain 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 arrays
Public bool returnsReference ()
// Test whether the function returns a reference.
Public ReflectionParameter [] getParameters ()
// Obtain the parameters required for this method. the returned value is an array of objects.
Public int getNumberOfParameters ()
// Obtain the number of parameters required for this method
Public int getNumberOfRequiredParameters ()
// Obtain the number of parameters required for this method
}
?>

④ ReflectionParameter class:
<? Php
Class ReflectionParameter implements Reflector
{
Final private _ clone ()
Public object _ construct (string name)
Public string _ toString ()
Public static string export ()
// Export the details of this parameter
Public string getName ()
// Obtain the parameter name
Public bool isPassedByReference ()
// Test whether the parameter is passed by reference.
Public ReflectionClass getClass ()
// If this parameter is an object, the class name of the object is returned.
Public bool isArray ()
// Test whether the parameter is of the array type.
Public bool allowsNull ()
// Test whether the parameter can be empty.
Public bool isOptional ()
// Test whether this parameter is optional. this parameter is optional when a default parameter exists.
Public bool isdefavaluvalueavailable ()
// Test whether this parameter is a default parameter
Public mixed getDefaultValue ()
// Obtain the default value of this parameter
}
?>

⑤ ReflectionClass class:
<? Php
Class ReflectionClass implements Reflector
{
Final private _ clone ()
Public object _ construct (string name)
Public string _ toString ()
Public static string export ()
// Export the detailed information of the class
Public string getName ()
// Obtain the class name or interface name
Public bool isInternal ()
// Test whether the 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 specified constant exists.
Public bool hasMethod (string name)
// Test whether the specified method exists.
Public bool hasProperty (string name)
// Test whether the class has specific attributes.
Public string getFileName ()
// Obtain the name of the file defining the class, including the path name
Public int getStartLine ()
// Get the start line that defines the class
Public int getEndLine ()
// Obtain the end row that defines the class
Public string getDocComment ()
// Obtain comments of this class
Public ReflectionMethod getConstructor ()
// Obtain the constructor information of this class
Public ReflectionMethod getMethod (string name)
// Obtain information about a specific method of the class
Public ReflectionMethod [] getMethods ()
// Obtain all the methods of the class
Public ReflectionProperty getProperty (string name)
// Obtain a specific property information
Public ReflectionProperty [] getProperties ()
// Obtain all attributes of the class
Public array getConstants ()
// Obtain all constants of this class
Public mixed getConstant (string name)
// Obtain information about a specific constant of this type
Public ReflectionClass [] getInterfaces ()
// Obtain 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 as final
Public int getModifiers ()
// Get the modifier of this class. The return value type may be a resource type.
// Use Reflection: getModifierNames ($ class-> getModifiers () to further read
Public bool isInstance (stdclass object)
// Test whether the input object is an instance of this class
Public stdclass newInstance (mixed * args)
// Create an instance of this type
Public ReflectionClass getParentClass ()
// Obtain the parent class
Public bool isSubclassOf (ReflectionClass)
// Test whether the passed class is the parent class of the class
Public array getStaticProperties ()
// Obtain all static attributes of the class
Public mixed getStaticPropertyValue (string name [, mixed default])
// Obtain the static property value of this class. if private, the value cannot be accessed.
Public void setStaticPropertyValue (string name, mixed value)
// Set the static property value of this class. if private, the value cannot be accessed, which is contrary to the encapsulation principle.
Public array getDefaultProperties ()
// Obtain the attributes 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:
<? Php
Class ReflectionMethod extends ReflectionFunction
{
Public _ construct (mixed class, string name)
Public string _ toString ()
Public static string export ()
// Export the information of 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 this 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 an destructor.
Public int getModifiers ()
// Get the modifier of this method
Public ReflectionClass getDeclaringClass ()
// Obtain the class to which the 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 ()
}
?>

7. ReflectionProperty class:
<? Php
Class ReflectionProperty implements Reflector
{
Final private _ clone ()
Public _ construct (mixed class, string name)
Public string _ toString ()
Public static string export ()
// Export the details of this attribute
Public string getName ()
// Obtain the attribute name
Public bool isPublic ()
// Test whether the attribute name is public
Public bool isPrivate ()
// Test whether the attribute name is private
Public bool isProtected ()
// Test whether the attribute name is protected
Public bool isStatic ()
// Test whether the property name is static
Public bool isDefault ()
Public int getModifiers ()
// Get modifier
Public mixed getValue (stdclass object)
// Obtain the attribute value
Public void setValue (stdclass object, mixed value)
// Set the attribute value
Public ReflectionClass getDeclaringClass ()
// Obtain the class that defines this attribute
Public string getDocComment ()
// Get the annotation of this attribute
}
?>

⑧ ReflectionExtension class
<? Php
Class ReflectionExtension implements Reflector {
Final private _ clone ()
Public _ construct (string name)
Public string _ toString ()

Public static string export ()
// Export all information about the extension
Public string getName ()
// Obtain the extension name
Public string getVersion ()
// Obtain the extended version
Public ReflectionFunction [] getFunctions ()
// Obtain all functions of the extension
Public array getConstants ()
// Obtain all constants of the extension
Public array getINIEntries ()
// Obtain the command information related to the extension in php. ini.
Public ReflectionClass [] getClasses ()
Public array getClassNames ()
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.