PHP reflection usage examples and Chinese description of PHP reflection API_PHP tutorial

WBOY
Release: 2016-07-13 10:26:27
Original
828 people have browsed it

Recently during the development process, I need to obtain the number, name and order of parameters of a certain class method, so that I can get the value from $_GET based on the name of the parameters.

If the method prototype is test($uid,$score), then I know that I need to get it from $_GET

Copy code The code is as follows:

$uid = $_GET['uid'];

$score = $_GET['score'];

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

Of course, the premise is that the parameter name is agreed to be consistent with the name of the value variable passed by the get method.

Using PHP's reflection API, the method to obtain the function parameter name and parameter default value is as follows:

Copy code The code is as follows:

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 is an introduction to the PHP reflection API:

1. Purpose:

This extension analyzes PHP programs and exports or extracts detailed information about classes, methods, properties, parameters, etc., including comments.
Reflection can be said to be an extension of the PHP library function: "Classes/Objects class/object function".
It is mainly used to detect information about classes, methods and other information inside existing PHP programs through programs and process them.

2. API overview:


Copy code 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. Detailed instructions: (see the php manual for examples)


Copy code The code is as follows:

①Reflection class
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 properties.

③ReflectionFunction class
class ReflectionFunction implements Reflector
{
final private __clone()
public object __construct(string name)
public string __toString()
public static string export()
//Export detailed information of the 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 where the function is defined
public int getEndLine()
//Get the end line of the defined function
public string getDocComment()
//Get the comments 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 for this method
public int getNumberOfRequiredParameters()
//Get the number of parameters required for this method
}
?>

④ReflectionParameter class:
class ReflectionParameter implements Reflector
{
final private __clone()
public object __construct(string name)
public string __toString()
public static string export()
//Export detailed information of this parameter
public string getName()
//Get parameter name
public bool isPassedByReference()
//Test whether the parameter is passed by reference
public ReflectionClass getClass()
//If the parameter is an object, return the class name of the object
public bool isArray()
//Test whether the parameter is an array type
public bool allowsNull()
//Test whether the parameter is allowed to be empty
public bool isOptional()
//Test whether the parameter is optional, optional when there is a default parameter
public bool isDefaultValueAvailable()
//Test whether the parameter is the default parameter
public mixed getDefaultValue()
//Get the default value of this parameter
}
?>

⑤ReflectionClass class:
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 the 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, which 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 class:
class ReflectionProperty implements Reflector
{
final private __clone()
public __construct(mixed class, string name)
public string __toString()
public static string export()
//Export detailed information of this attribute
public string getName()
//Get the attribute name
public bool isPublic()
//Test whether the property 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 attribute name is static
public bool isDefault()
public int getModifiers()
//Get modifier
public mixed getValue(stdclass object)
//Get the attribute value
public void setValue(stdclass object, mixed value)
//Set the attribute value
public ReflectionClass getDeclaringClass()
//Get the class that defines this attribute
public string getDocComment()
//Get the comment of this attribute
}
?>

⑧ReflectionExtension class
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()
//Get the name of the extension
public string getVersion()
//Get the version of the extension
public ReflectionFunction[] getFunctions()
//Get all functions of this extension
public array getConstants()
//Get all constants of this extension
public array getINIEntries()
//Get the command information in php.ini related to the extension
public ReflectionClass[] getClasses()
public array getClassNames()
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/824666.htmlTechArticleRecently, during the development process, I need to obtain the number, name and order of parameters of a certain class method, so that according to the parameters Name to get the value from $_GET. If the method prototype is test($uid,$score), then...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template