1.使用映射执行授权模式
class ClassOne {
function callClassOne() {
print "in class one\n";
}
}
class ClassTwo {
function callClassTwo() {
print "in class two";
}
}
class ClassOneDelgater {
private $target;
function __construct() {
$this->target[] = new ClassOne();
}
function addObject($obj) {
$this->target[] = $obj;
}
function __call($name,$args) {
foreach ($this->target as $obj) {
$r = new ReflectionClass($obj);
try {
if($method = $r->getMethod($name)) {
if($method->isPublic()) {
return $method->invoke($obj,$args);
}
}
}catch(Exception $e) {
//print $e->getMessage();
}
}
}
}
$obj = new ClassOneDelgater();
$obj->addObject(new ClassTwo());
//var_dump($obj->target);
$obj->callClassOne();
$obj->callClassTwo();
结果是
in class one
in class two
2.函数,类,变量
php遇到未定义的变量或者常量时会继续运行,但是它一旦遇到未定义的函数或者类时就会终止执行
在类中有一个特例。如果用户定义了__autoload函数,他在php调用未定义的类时,会通过__autoload返回该类的定义,新加载的类将会被使用,不会出错
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。