Zihao

Make small but daily progress

0%

PHP反射机制实现代理模式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* Created by PhpStorm.
* User: zihao
* Date: 2017/5/10
* Time: 下午5:02
*/
class test{
function callprint(){
print_r("onesdf");
}
function test2( $args = "" ){
print_r( $args );
}
}

class testDelegator{
private $targets;
function __construct($obj){
$this->targets[] = $obj;
}
function __call($name,$args){
foreach($this->targets as $obj){
$r = new ReflectionClass($obj);
if($method=$r->getMethod($name)){
if($method->isPublic() && !$method->isAbstract()){
return $method->invoke($obj,$args);
}
}
}
}
}

$d = new testDelegator(new test());
$d->callprint();
$d->test2('the name of the method is test2','array2');
  • 本文作者: Zihao Yao
  • 本文链接: https://yaozihao.com/php_delegator/
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

欢迎关注我的其它发布渠道