1. 什么是__call魔术函数* *
–call() 是php对象调用函数时,如果没有找到对应的函数,则调用的函数,类似于前端的钩子函数,该函数会接收对象传过来的方法名以及方法参数。
2.什么是映射?
类似于java的映射,可以间接的调用别的对象的方法和参数;
3.两种方式相结合,可以调用别的对象的方法
例子:
class one {
public function classone()
{
print 'classone';
}
}
class two {
public function classtow()
{
print 'classtow';
}
}
class De{
private $obj = [];
function __construct()
{
$this->obj[] = new one();
}
public function add($objs)
{
$this->obj[] = $objs;
}
function __call($name, $args)
{
foreach ($this->obj as $obj)
{
$r = new ReflectionClass($obj);
if($r->hasMethod($name))
{
$method = $r->getMethod($name);
if($method->isPublic()&&!$method->isAbstract())
{
return $method->invoke($obj,$args);
}
}
}
}
}
$d = new De();
$d->add(new two());
$d->classtow();
$d->classone();```