1
익명의 함수 콜백을 사용하여 라우터를 작성했습니다.익명 함수의 범위
샘플 작은 코드에 대한
$this->getRouter()->addRoute('/login', function() {
Controller::get('login.php', $this);
});
$this->getRouter()->addRoute('^/activate/([a-zA-Z0-9\-]+)$', function($token) {
Controller::get('activate.php', $this);
});
, 내가 배열로 이동합니다.
내가 다음 methdos로 라우팅 클래스를 작성했다 :
<?php
namespace CTN;
class Routing {
private $path = '/';
private $controller = NULL;
public function __construct($path, $controller = NULL) {
$this->path = $path;
$this->controller = $controller;
}
public function getPath() {
return $this->path;
}
public function hasController() {
return !($this->controller === NULL);
}
public function getController() {
return $this->controller;
}
}
?>
을 그리고 내 배열이 새 클래스와 라우팅 경로가 :
foreach([
new Routing('/login', 'login.php'),
new Routing('^/activate/([a-zA-Z0-9\-]+)$', 'activate.php');
] AS $routing) {
// Here, $routing is available
$this->getRouter()->addRoute($routing->getPath(), function() {
// SCOPE PROBLEM: $routing is no more available
if($routing->hasController()) { // Line 60
Controller::get($routing->getController(), $this);
}
});
}
나의 현재 문제가 (주석 참조), $routing
변수는 익명의 기능을 사용할 수 없습니다.
Fatal error: Call to a member function hasController() on null in /core/classes/core.class.php on line 60
어떻게이 문제를 해결할 수 있습니까?
당신은 "사용"을 사용하여 부모 범위에서 변수를 사용할 수 있습니다
오 "부모 범위에서 3 상속 변수 예 #" , 물론! 빠른 답변 주셔서 감사합니다, 그것은 작동 :) –