PHP reverse image mechanism explanation and plug-in architecture implementation

Source: Internet
Author: User
Tags reflector
PHP Reflection mechanism details and plug-in architecture implementation 1. Purpose: this extension analysis php program 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 information about classes and methods in existing php programs through programs, and to provide PHP Reflection mechanism details and plug-in architecture implementation.
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:
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. For more information, see The php Manual)
① Reflection class
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
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:
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:
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:
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:
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
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 ()
}
?>

4. Appendix:

In fact, we can see from the second API overview that the interface is quite useful.
On the one hand, the Reflector interface provides a good interface naming standard for the Reflection small system,
Every class that implements it must follow its specifications. from the external perspective, this small system API is very unified.
On the other hand, because many classes implement the Reflector interface,
In such a class hierarchy, it is easy to implement polymorphism.



Demo CodeAs follows:
Class ClassOne {
Function callClassOne (){
Print "In Class One ";
}
}
Class ClassOneDelegator {
Private $ targets;
Function _ construct (){
$ This-> target [] = new ClassOne ();
}
Function _ call ($ name, $ args ){
Foreach ($ this-> target as $ obj ){
$ R = new ReflectionClass ($ obj );
If ($ method = $ r-> getMethod ($ name )){
If ($ method-> isPublic ()&&! $ Method-> isAbstract ()){
Return $ method-> invoke ($ obj, $ args );
}
}
}
}
}
$ Obj = new ClassOneDelegator ();
$ Obj-> callClassOne ();
?>

Output result:
In Class One
Visible, through ProxyClass ClassOneDelegator instead of ClassOne class to implement its Method.
Similarly, the following code can be run:
Class ClassOne {
Function callClassOne (){
Print "In Class One ";
}
}
Class ClassOneDelegator {
Private $ targets;
Function addObject ($ obj ){
$ This-> target [] = $ obj;
}
Function _ call ($ name, $ args ){
Foreach ($ this-> target as $ obj ){
$ R = new ReflectionClass ($ obj );
If ($ method = $ r-> getMethod ($ name )){
If ($ method-> isPublic ()&&! $ Method-> isAbstract ()){
Return $ method-> invoke ($ obj, $ args );
}
}
}
}
}
$ Obj = new ClassOneDelegator ();
$ Obj-> addObject (new ClassOne ());
$ Obj-> callClassOne ();
?>

PHP Reflection API-plug-in system architecture implemented by reflection technology
Http://www.e897.com/Article/wlbc/php/200908/5377.html

???? 1 ./**
???? 2. * @ name ???? PHP Reflection API-plug-in system architecture implemented by reflection technology
???? 3. * @ author: PHPCQ. COM
???? 4 .*/????
???? 5. interface Iplugin {????
???? 6 .???????????????? Public static function getName ();????
???? 7 .}????
???? 8. function findPlugins (){????
???? 9 .???????????????? $ Plugins = array ();????
???? 10 .???????????????? Foreach (get_declared_classes () as $ class ){????
???? 11 .???????????????????????????????? $ ReflectionClass = new ReflectionClass ($ class );????
???? 12 .???????????????????????????????? If ($ reflectionClass-> implementsInterface ('iplugin ')){????
???? 13 .?????????????????????????????????????? ?????????? $ Plugins [] = $ reflectionClass ;????
???? 14 .???????????????????????????????? }????
???? 15 .???????????????? }????
???? 16 .???????????????? Return $ plugins ;????
???? 17 .}????
???? 18. function computeMenu (){????
???? 19 .???????????????? $ Menu = array ();????
???? 20 .???????????????? Foreach (findPlugins () as $ plugin ){????
???? 21 .???????????????????????????????? If ($ plugin-> hasMethod ('getmenuitems ')){????
???? 22 .?????????????????????????????????????? ?????????? $ ReflectionMethod = $ plugin-> getMethod ('getmenuitems ');????
???? 23 .?????????????????????????????????????? ?????????? If ($ reflectionMethod-> isStatic ()){????
???? 24 .?????????????????????????????????????? ?????????????????????????? $ Items = $ reflectionMethod-> invoke (null );????
???? 25 .?????????????????????????????????????? ?????????? } Else {????
???? 26 .?????????????????????????????????????? ?????????????????????????? $ PluginInstance = $ plugin-> newInstance ();????
???? 27 .?????????????????????????????????????? ?????????????????????????? $ Items = $ reflectionMethod-> invoke ($ pluginInstance );????
???? 28 .?????????????????????????????????????? ?????????? }????
???? 29 .?????????????????????????????????????? ?????????? $ Menu = array_merge ($ menu, $ items );????
???? 30 .???????????????????????????????? }????
???? 31 .???????????????? }????
???? 32 .???????????????? Return $ menu ;????
???? 33 .}????
???? 34. function computeArticles (){????
???? 35 .???????????????? $ Articles = array ();????
???? 36 .???????????????? Foreach (findPlugins () as $ plugin ){????
???? 37 .???????????????????????????????? If ($ plugin-> hasMethod ('getarticles ')){????
???? 38 .?????????????????????????????????????? ?????????? $ ReflectionMethod = $ plugin-> getMethod ('getarticle ');????
???? 39 .?????????????????????????????????????? ?????????? If ($ reflectionMethod-> isStatic ()){????
???? 40 .?????????????????????????????????????? ?????????????????????????? $ Items = $ reflectionMethod-> invoke (null );????
???? 41 .?????????????????????????????????????? ?????????? } Else {????
???? 42 .?????????????????????????????????????? ?????????????????????????? $ PluginInstance = $ plugin-> newInstance ();????
???? 43 .?????????????????????????????????????? ?????????????????????????? $ Items = $ reflectionMethod-> invoke ($ pluginInstance );????
???? 44 .?????????????????????????????????????? ?????????? }????
???? 45 .?????????????????????????????????????? ?????????? $ Articles = array_merge ($ articles, $ items );????
???? 46 .???????????????????????????????? }????
???? 47 .???????????????? }????
???? 48 .???????????????? Return $ articles ;????
???? 49 .}????
???? 50. require_once ('In in. php ');????
???? 51. $ menu = computeMenu ();????
???? 52. $ articles ???? = ComputeArticles ();????
???? 53. print_r ($ menu );????
???? 54. print_r ($ articles );????
???? 55 .????
???? 56 .????
???? 57. // The plugin. php code is as follows ????
???? 58. ???? 59. class MycoolPugin implements Iplugin {????
???? 60 .???????????????? Public static function getName (){????
???? 61 .???????????????????????????????? Return 'myolplugin ';????
???? 62 .???????????????? }????
???? 63 .???????????????? Public static function getMenuItems (){????
???? 64 .???????????????????????????????? Return array ('description' => 'myolin in', 'link' => '/mycoolin in '));????
???? 65 .???????????????? }????
???? 66 .???????????????? Public static function getArticles (){????
???? 67 .???????????????????????????????? Return array ('path' => '/mycoolin in', 'title' => 'This is a really cool article ', 'text' => xxxxxxxxx ));????
???? 68 .???????????????? }????
???? 69 .}



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.