Recently, in the development process, we need to get the parameter number, name and parameter order of a class method, so that we can take the value from $_get according to the name of the parameter.
If the method prototype is test ($uid, $score), then I know I need to take from $_get
Copy the Code code as follows:
$uid = $_get[' uid '];
$score = $_get[' score ');
Then call method $obj->test ($uid, $score)
Of course, the assumption is that the parameter name and the Get method pass the value variable name consistent.
Using the PHP reflection API, get the function parameter name and parameter default values as follows:
Copy the Code code as follows:
<?php
Class testclass{
Public Function TestFunc ($param 1, $param 2=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. Use:
The extension parses the PHP program, exporting or extracting detailed information about classes, methods, properties, parameters, and so on, including comments.
Reflection can be said to be an extension of the PHP library function: "Classes/objects class/Object function".
Mainly used in the program to detect the existing PHP procedures within the class, methods and other information, and make processing.
2. API Overview:
Copy the Code code 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 Description: (See the PHP Manual for examples)
Copy CodeThe code is as follows:
①reflection class
<?php
Class Reflection
{
public static mixed export (Reflector R [, BOOL return])
To export the details of a class or method
public static array getmodifiernames (int modifiers)
Gets the name of the modifier
}
?>
②reflectionexception class
The class inherits from the standard class and has no special methods and properties.
③reflectionfunction class
<?php
Class Reflectionfunction implements Reflector
{
Final Private __clone ()
public Object __construct (string name)
public string __tostring ()
public static string export ()
Export the details of the function
public string GetName ()
The name of the letter obtained
public bool IsInternal ()
Test whether the system intrinsic function
public bool Isuserdefined ()
Test whether a user-defined function
public string GetFileName ()
Get file name, including path name
public int Getstartline ()
Gets the starting line that defines the function
public int Getendline ()
Gets 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 to pass parameters through the argument list
Public mixed Invokeargs (array args)
Call the function, pass the parameter through the array
public bool Returnsreference ()
Tests whether the function returns a reference
Public reflectionparameter[] GetParameters ()
Get the parameters required for the method, the return value is an array of objects
public int getnumberofparameters ()
The number of arguments required to obtain the method
public int getnumberofrequiredparameters ()
The number of arguments required to obtain the 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 ()
Get the name of the parameter
public bool Ispassedbyreference ()
Tests whether the parameter is passed by reference
Public Reflectionclass GetClass ()
If the argument is an object, returns the class name of the object
public bool IsArray ()
Tests whether the parameter is an array type
public bool Allowsnull ()
Tests whether this parameter is allowed to be empty
public bool Isoptional ()
Tests whether the parameter is optional and optional when there are default parameters
public bool Isdefaultvalueavailable ()
Test whether the parameter is a default parameter
Public mixed Getdefaultvalue ()
Get the default value for 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 details of this class
public string GetName ()
Get the class name or interface name
public bool IsInternal ()
Tests whether the class is a system-internal class
public bool Isuserdefined ()
Test whether the class is a user-defined class
public bool Isinstantiable ()
Tests whether the class has been instantiated
public bool HasConstant (string name)
Test if the class has a specific constant
public bool Hasmethod (string name)
Test whether the class has a specific method
public bool Hasproperty (string name)
To test whether the class has a specific property
public string GetFileName ()
Gets the file name that defines the class, including the path name
public int Getstartline ()
Gets the start line that defines the class
public int Getendline ()
Gets the end line that defines the class
public string getdoccomment ()
Get comments for this class
Public Reflectionmethod GetConstructor ()
Gets the constructor information for this class
Public Reflectionmethod GetMethod (string name)
Get a specific method information for this class
Public reflectionmethod[] GetMethods ()
Get all the method information for the class
Public Reflectionproperty GetProperty (string name)
To obtain a specific attribute information
Public reflectionproperty[] GetProperties ()
Get all property information for this class
Public array getconstants ()
Get all constant information for this class
Public mixed Getconstant (string name)
Get the specific constant information for this class
Public reflectionclass[] Getinterfaces ()
Get interface class Information
public bool Isinterface ()
Tests whether the class is an interface
public bool IsAbstract ()
To test whether the class is an abstract class
public bool IsFinal ()
Tests whether the class is declared final
public int getmodifiers ()
Gets the modifier for the class, the return value type may be a resource type
Further reading via Reflection::getmodifiernames ($class->getmodifiers ())
public bool Isinstance (Stdclass object)
Tests whether an incoming object is an instance of the class
Public Stdclass newinstance (mixed* args)
Create the class instance
Public Reflectionclass Getparentclass ()
Get parent class
public bool IsSubclassOf (Reflectionclass Class)
Tests whether the passed-in class is the parent class of the class
Public array getstaticproperties ()
Gets all the static properties of the class
Public mixed Getstaticpropertyvalue (string name [, mixed default])
Gets the static property value of the class, which is not accessible if private
public void Setstaticpropertyvalue (string name, mixed value)
Set the static property value of the class, if private, is inaccessible, contrary to the encapsulation principle
Public array getdefaultproperties ()
Gets the property information for the class, without static properties
public bool Isiterateable ()
public bool Implementsinterface (string name)
Tests whether a particular interface was 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 information for this method
Public mixed Invoke (Stdclass object, mixed* args)
Call this method
Public mixed Invokeargs (Stdclass object, array args)
Call this method, pass multi-parameter
public bool IsFinal ()
Tests whether the method is final
public bool IsAbstract ()
Tests whether the method is abstract
public bool IsPublic ()
Test whether the method is public
public bool IsPrivate ()
Tests whether the method is private
public bool Isprotected ()
Test whether the method is protected
public bool IsStatic ()
Tests whether the method is static
public bool Isconstructor ()
Tests whether the method is a constructor
public bool Isdestructor ()
Test whether the method is a destructor
public int getmodifiers ()
Gets the modifier for the method
Public Reflectionclass Getdeclaringclass ()
Gets 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 ()
}
?>
⑦reflectionproperty class:
<?php
Class Reflectionproperty implements Reflector
{
Final Private __clone ()
Public __construct (mixed class, string name)
public string __tostring ()
public static string export ()
Export details of this property
public string GetName ()
Gets the property name
public bool IsPublic ()
Tests whether the property name is public
public bool IsPrivate ()
Tests whether the property name is private
public bool Isprotected ()
Tests whether the property name is protected
public bool IsStatic ()
Tests whether the property name is static
public bool IsDefault ()
public int getmodifiers ()
Get modifier
Public mixed GetValue (Stdclass object)
Gets the property value
public void SetValue (Stdclass object, mixed value)
Set this property value
Public Reflectionclass Getdeclaringclass ()
Gets the class that defines the property
public string getdoccomment ()
Get the comment for this property
}
?>
⑧reflectionextension class
<?php
Class Reflectionextension implements Reflector {
Final Private __clone ()
Public __construct (string name)
public string __tostring ()
public static string export ()
Export all information for this extension
public string GetName ()
Get the name of the extension
public string GetVersion ()
Get the version of the extension
Public reflectionfunction[] Getfunctions ()
Get all the functions of the extension
Public array getconstants ()
Get all constants for this extension
Public array getinientries ()
Gets the instruction information in the php.ini associated with the extension
Public reflectionclass[] Getclasses ()
Public array getclassnames ()
}
?>
http://www.bkjia.com/PHPjc/824666.html www.bkjia.com true http://www.bkjia.com/PHPjc/824666.html techarticle recently, in the development process, we need to get the parameter number, name and parameter order of a class method, so that we can take the value from $_get according to the name of the parameter. If the method prototype is test ($uid, $score), then ...