Recently in the development process need to get a class method of the number of parameters, name and parameter order, based on the name of the parameter from the $_get value.
If the method prototype is test ($uid, $score), then I know that it needs to be taken from $_get
Copy Code code as follows:
$uid = $_get[' uid '];
$score = $_get[' score '];
Then call method $obj->test ($uid, $score)
Of course, the premise is that the parameter name and get method pass the value variable name consistent.
Using the Reflection API of PHP, the method of obtaining the function parameter name and parameter default value is as follows:
Copy 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 analyzes 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 adoption of procedures to detect the existing PHP program inside the class, methods and other information, and make the deal.
2, API Overview:
Copy 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: (for example, see PHP Manual)
Copy Code code 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)
Gets the name of the modifier
}
?>
②reflectionexception class
This class inherits from the standard class, with 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 ()
To export the function details
public string GetName ()
Get the name of the letter
public bool Isinternal ()
Test for system internal functions
public bool Isuserdefined ()
Testing whether user-defined functions
public string GetFileName ()
Gets the file name, including the path name
public int Getstartline ()
Gets the start line of the defined function
public int Getendline ()
Gets the end line of the defined function
public string getdoccomment ()
Get a comment on a function
Public array getstaticvariables ()
Get static variables
Public mixed Invoke (mixed* args)
Call this function to pass parameters by argument list
Public mixed Invokeargs (array args)
Call the function, pass the parameter through the array
public bool Returnsreference ()
Test whether the function returns a reference
Public reflectionparameter[] GetParameters ()
Gets the parameters required by the method, and the return value is an array of objects
public int getnumberofparameters ()
The number of parameters required to get the method
public int getnumberofrequiredparameters ()
The number of parameters required to get 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 details for this parameter
public string GetName ()
Get the name of the parameter
public bool Ispassedbyreference ()
Test 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 ()
Test whether the parameter is an array type
public bool Allowsnull ()
Test whether this parameter is allowed to be null
public bool Isoptional ()
Tests whether the parameter is optional and optional when a default parameter is available
public bool Isdefaultvalueavailable ()
Test whether the parameter is a default parameter
Public mixed Getdefaultvalue ()
Gets 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 the details of this class
public string GetName ()
Gets 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 ()
Tests whether the class has been instantiated
public bool HasConstant (string name)
To test whether 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 specific properties
public string GetFileName ()
Gets the name of the file 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 the class
Public Reflectionmethod GetMethod (string name)
To obtain a specific method information for this class
Public reflectionmethod[] GetMethods ()
Gets all of the method information for the class
Public Reflectionproperty GetProperty (string name)
To get a specific property information
Public reflectionproperty[] GetProperties ()
Get all the property information for this class
Public array getconstants ()
Get all constant information for this class
Public mixed Getconstant (string name)
Gets the specific constant information for this class
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 ()
Gets the modifier for the class, and the return value type may be a resource type
Read further through Reflection::getmodifiernames ($class->getmodifiers ())
public bool Isinstance (Stdclass object)
Tests whether the incoming object is an instance of the class
Public Stdclass newinstance (mixed* args)
Create an instance of this class
Public Reflectionclass Getparentclass ()
Get parent class
public bool IsSubclassOf (Reflectionclass Class)
Tests whether the incoming 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 not accessible, is inconsistent with the encapsulation principle
Public array getdefaultproperties ()
Gets the property information for the class, excluding static properties
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 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 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 ()
Gets the modifier for the method
Public Reflectionclass Getdeclaringclass ()
Gets the class that the method belongs to
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 this property for more information
public string GetName ()
Gets the property name
public bool IsPublic ()
Test whether the property name is public
public bool IsPrivate ()
Test whether the property name is private
public bool Isprotected ()
Test whether the property name is protected
public bool IsStatic ()
Test whether the property name is static
public bool IsDefault ()
public int getmodifiers ()
Get modifiers
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 a 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 this extension
Public reflectionfunction[] Getfunctions ()
Gets 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 ()
}
?>