Home > Backend Development > PHP Tutorial > PHP reflection mechanism code to implement dynamic proxy_PHP tutorial

PHP reflection mechanism code to implement dynamic proxy_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-21 15:49:01
Original
796 people have browsed it

The code for demonstration is as follows:

Copy code The code is as 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
is visible, replaced by the proxy class ClassOneDelegator ClassOne class to implement his methods.
Similarly, the following code can also be run:
Copy the code The code is as follows:

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();
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/319636.htmlTechArticleThe demonstration code is as follows: Copy the code as follows: ?php class ClassOne { function callClassOne() { print " In Class One"; } } class ClassOneDelegator { private $targets; funct...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template