kin*_*kee 4 php oop methods call
我目前正在开发一个自己的PHP-MVC-Framework(仅用于经验目的).
我的问题:每次调用类方法时,是否可以调用已定义的函数或方法?
例如:
public function view($id) {
//Code ...
$this->view->render(__FUNCTION__);
}
Run Code Online (Sandbox Code Playgroud)
我想要的是:
public function view($id) {
//Code ...
//render-method is called automatically with functionname as parameter
}
Run Code Online (Sandbox Code Playgroud)
我尝试了不同的方法......但没有成功.如果有人可以帮我解决这个问题会很棒.
干杯,克里斯
您可以使用Magic Methods来实现此行为:
public function __call($func, $args) {
if(!method_exists($this, $func)) {
return;
}
// do some coding here
call_user_func_array($func,$args);
// do some coding there
}
private function view($arg1, $arg2) {
// and here
}
Run Code Online (Sandbox Code Playgroud)
请记住:视图功能必须是私有/受保护的.
$obj->view("asdasd", "asdsad");
Run Code Online (Sandbox Code Playgroud)
应该做:: __ call(),然后:: view()方法