Analysis of the application _php techniques of reflection in PHP

Source: Internet
Author: User
Tags reflection
Use of one reflex:
Copy Code code 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), and/or instantiated Reflectionclass according to the name of the configuration ConfigData
if (! $module _class->issubclassof ($interface)) {//check reflection to see if the class is a $interface subclass
throw new Exception ("Unknown module type: $modulename")//Is not a module subclass throws an exception
}
$module = to instantiate a ftpmodule or Personmodule object $module _class->newinstance ();//
foreach ($module _class->getmethods () as $method) {//Get Methods in class
$this->handlemethod ($module, $method, $params);
}
Array_push ($this->modules, $module);//Insert the instantiated module object into the $modules array
}
}
function Handlemethod (Module $module, Reflectionmethod $method, $params) {
$name = $method->getname ()//Get Method name
$args = $method->getparameters ()//Get the parameters in the method
if (count ($args)!=1| | substr ($name, 0,3)!= "set") {//Check method must start with set with only one argument
return false;
}
$property =strtolower (substr ($name, 3))//The method name removes the set three letters as a parameter
if (!isset ($params [$property])) {//If $params array does not contain a property, returns false
return false;
}
$arg _class=@ $args [0]->getclass;//Check the data type of the first parameter (and only) 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, the information in the class is obtained by reflection:
Copy Code code 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: Class person{public $age, public $name, function GetName () {return ' ZJX ';} function Getage () {return} function __to String () {$rs = $this->getname () $rs. = "(age. $this->getage ()."); return $rs}}
<PRE></PRE>
<PRE></PRE>
<PRE></PRE>
<PRE></PRE>

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.