1. Use of reflection:
Copy codeThe Code is as follows:
<? Php
Class Person {
Public $ name;
Function _ construct ($ name ){
$ This-> name = $ name;
}
}
Interface Module {
Function execute ();
}
Class FtpModule implements Module {
Function setHost ($ host ){
Print "FtpModule: setHost (): $ host \ n ";
}
Function setUser ($ user ){
Print "FtpModule: setUser (): $ user \ n ";
}
Function execute (){
// Something
}
}
Class PersonModule implements Module {
Function setPerson (Person $ person ){
Print "PersonModule: setPerson: {$ person-> name} \ n ";
}
Function execute (){
// Something
}
}
Class ModuleRunner {
Private $ configData
= Array (
"PersonModule" => array ('person '=> 'bob '),
"FtpModule" => array ('host' => 'example. com', 'user' => 'anon ')
);
Private $ modules = array ();
Function init (){
$ Interface = new ReflectionClass ('module ');
Foreach ($ this-> configData as $ modulename => $ params ){
$ Module_class = new ReflectionClass ($ modulename); // instantiate ReflectionClass Based on the configured configData name
If (! $ Module_class-> isSubclassOf ($ interface) {// check whether the reflection result shows whether the class is a subclass of $ interface.
Throw new Exception ("unknown module type: $ modulename"); // if it is not a Module subclass, an Exception is thrown.
}
$ Module = $ module_class-> newInstance (); // instantiate an FtpModule or PersonModule object
Foreach ($ module_class-> getMethods () as $ method) {// obtain methods in the class
$ This-> handleMethod ($ module, $ method, $ params );
}
Array_push ($ this-> modules, $ module); // put the instantiated module object into the $ modules array.
}
}
Function handleMethod (Module $ module, ReflectionMethod $ method, $ params ){
$ Name = $ method-> getName (); // obtain the method name
$ Args = $ method-> getParameters (); // obtain parameters in the method
If (count ($ args )! = 1 | substr ($ name, 0, 3 )! = "Set") {// check method must start with set and have only one parameter
Return false;
}
$ Property = strtolower (substr ($ name, 3); // remove the set three letters from the method name as a parameter.
If (! Isset ($ params [$ property]) {// if the $ params array does not contain an attribute, false is returned.
Return false;
}
$ Arg_class = @ $ args [0]-> getClass; // check the data type of the first parameter (and unique) of the setter method.
If (empty ($ arg_class )){
$ Method-> invoke ($ module, $ params [$ property]);
} Else {
$ Method-> invoke ($ module, $ arg_class-> newInstance ($ params [$ property]);
}
}
}
$ Test = new ModuleRunner ();
$ Test-> init ();
?>
Second, obtain information in the class through reflection:
Copy codeThe Code is as follows:
<PRE class = php name = "code"> <? Php
Class ReflectionUtil {
Static function getClassSource (ReflectionClass $ class ){
$ Path = $ class-> getFileName ();
$ Lines = @ file ($ path );
$ From = $ class-> getStartLine ();
$ To = $ class-> getEndLine ();
$ Len = $ to-$ from + 1;
Return implode (array_slice ($ lines, $ from-1, $ len ));
}
}
$ Classname = "Person ";
$ Path = "../practice/{$ classname}. php ";
If (! File_exists ($ path )){
Throw new Exception ("No such file as {$ path }");
}
Require_once ($ path );
If (! Class_exists ($ classname )){
Throw new Exception ("No such class as {$ classname }");
}
Print ReflectionUtil: getClassSource (new ReflectionClass ('person '));
?>
</PRE> <BR>
<PRE> </PRE>
The result is: class Person {public $ age; public $ name; function getName () {return "zjx" ;}function getAge () {return 12 ;}function _ toString () {$ rs = $ this-> getName (); $ rs. = "(age ". $ this-> getAge (). ")"; return $ rs ;}}
<PRE> </PRE>
<PRE> </PRE>
<PRE> </PRE>
<PRE> </PRE>