Parse php Reflection applications. I. reflection usage: Copy the code as follows :? PhpclassPerson {public $ name; function _ construct ($ name) {$ this-name $ name ;}} interfaceModule {functionexecute ();} class
1. use of reflection:
The code is as follows:
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:
The code is as follows:
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'));
?>
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 ;}}
Use of callback reflection: the code is as follows :? Php class Person {public $ name; function _ construct ($ name) {$ this-name = $ name ;}} interface Module {function execute ();} class...