PHP 反射机制

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

?>



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

 

你可能感兴趣的:(PHP)