Home > Article > Backend Development > PHP Reflection API_PHP Tutorial
Understanding of reflection
It refers to extending the analysis of PHP programs in the running state of PHP, exporting or extracting detailed information about classes, methods, attributes, parameters, etc., and even comments. This function of dynamically obtaining information and dynamically calling methods of objects is called reflection API. Reflection is an API for manipulating meta-models in object-oriented models. It is very powerful and can help us build complex and scalable applications. (ps: including use in factory mode)
Reflection API is PHP's built-in oop technology extension, including some classes, exceptions and interfaces. Used together, they can be used to help us analyze other classes, interfaces, methods, properties and extensions. These oop extensions are called reflection.
ReflectionClass
[php]
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 this 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
//Read further 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()
}
?>
Factory mode application:
[php]
class MoveDataFactory
{
/**
* Description: Simple factory mode, select different instantiated objects according to mode
* @return object instance
*/
Public function GetMoveClass($classname)
{
$reflectionclass = new ReflectionClass($classname);
return $reflectionclass->newInstance();
}
}