我目前正在开发自己的 PHP-MVC 框架(仅供体验)。
我的问题:是否可以在每次调用类方法时调用已定义的函数或方法 被调用了吗?
例如:
public function view($id) {
//Code ...
$this->view->render(__FUNCTION__);
}
我想要的是:
public function view($id) {
//Code ...
//render-method is called automatically with functionname as parameter
}
我尝试了不同的方法……但没有成功。 如果有人可以帮助我解决这个问题,那就太好了。
干杯, 克里斯
最佳答案
您可以使用 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
}
记住: View 函数必须是私有(private)的/ protected 。
$obj->view("asdasd", "asdsad");
应该执行::__call(),然后执行::view() 方法
关于总是被调用的 PHP 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4539611/