---
title: "PHP-返射代理"
author: "leiwuluan"
date: "2013-12-22T11:49:03Z"
tags: []
categories: ["编程语言"]
slug: "php"
draft: false
lastmod: "2013-12-22T11:49:03Z"
id: 1993440
url: "https://leiwuluan.iteye.com/blog/1993440"
---

```
<?php

class Personal {
	public $name;
	public $age;
	
	function __construct() {
		echo " __construct.. \r\n";
	}
	
	public function init() {
		echo "init \r\n";
	}
}

interface Work {
 	function doWork($personal);
}
 
class Student extends Personal implements Work{
	public function doWork($personal)  {
		echo "$personal \n\r";
	}
}

 
 # 代理
 class ClassDelegator {
 	private $target;
 	
 	function __construct($targetClass) {
 		$this->target[] = new $targetClass();
 	}
 	
 	function __call($name, $args) {
 		foreach ($this->target as $obj) { 
			$r = new ReflectionClass($obj); 
			if ($method = $r->getMethod($name)) { 
				if ($method->isPublic() && !$method->isAbstract()) { 
					return call_user_func_array(array(&$obj, $name), $args);
				}
			}
		} 
 	}
 }
 
 $stu = new ClassDelegator('Student');
 $stu->doWork('1111', 'rerew');
 
 
 
 
 
 
 
 
```