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);
});
、私は、Arrayに移動します。
私は、次のmethdosでルーティングクラスを記述していた:(コメントを参照)
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);
}
});
}
私の現在の問題は次のとおりです。
<?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;
}
}
?>
そして、私の配列は、新しいクラスとのルーティングパスを持っています$routing
変数は匿名関数上にありません。
Fatal error: Call to a member function hasController() on null in /core/classes/core.class.php on line 60
どのようにこの問題を解決できますか?
ああで始まる部分を、確か!高速回答ありがとう、それは動作します:) –