1
폐쇄 나 예를 들어 사용할 수 있도록 객체를 반환PHP 폐쇄는
$app->register('test', function() { return 'test closure'; });
echo $app->test();
문제는, 폐쇄 개체를 반환 할 때 작동하지 않습니다. 마찬가지로 :
$app->register('router', function() { return new Router(); });
$app->router->map($url, $path);
내가 얻을 : Fatal error: Call to undefined method Closure::map() in index.php on line 22
/** app.php **/
class App {
public function __construct(){
$this->request = new Request();
$this->response = new Response();
}
public function register($key, $instance){
$this->$key = $instance;
}
public function __set($key, $val){
$this->$key = $val;
}
public function __get($key){
if($this->$key instanceof Closure){
return $this->$key();
}else return $this->$key;
}
public function __call($name, $args){
$closure = $this->$name;
call_user_func_array($closure, $args); // *
}
}
/** router.php **/
class Router {
public function map($url, $action){
}
}
부가 기능 세부 사항 :
합니까 일 :
$app->register('router', new Router());
$app->router->map($url, $action);
하지만 폐쇄에서 개체를 반환하는 목적이다 필요에 따라 최후의 구성을 제공합니다. 그러나 이것에 대한 연구를 시도했지만 대부분의 주제는 내가 이미 이해하고있는 클로저를 호출하는 방법을 설명하라. 응용 프로그램 클래스의 __call 방법이있는 이유는 ...입니다
편집 :
$app->router()->map($url, $action);
Fatal error: Call to a member function map() on null in
... 감사 –
그것은 실제로 가능하다 : 나는 당신이 원하는 것을 할 수있는 직접 구문이 있다고 생각하지 않는다, 당신은 같은 임시 변수를 통해 전달해야 메소드에서'call_user_func_array' 호출을 리턴해야했습니다. 이제, 다음과 같이 쓸 수 있습니다 :'$ app-> router() -> map ('url', 'action'); –