Php 多维关联数组
为什么这不会推迟或增加到最后 两种方法我都试过了Php 多维关联数组,php,Php,为什么这不会推迟或增加到最后 两种方法我都试过了 class Router { public function __construct() { $this->routes['GET'] = []; $this->routes['POST'] = []; } public function handle($request) { echo count($this->routes['GET'])
class Router
{
public function __construct()
{
$this->routes['GET'] = [];
$this->routes['POST'] = [];
}
public function handle($request)
{
echo count($this->routes['GET']) . "<br/>"; // outputs '0'.. why??
}
protected function addRoute($methods, $uri, $action)
{
#array_push($this->routes['GET'], 'TEST'); // Tried this as well
$this->routes['GET'][] = 'test';
echo count($this->routes['GET']) . "<br/>"; // outputs '1'
}
private $routes = [];
}
类路由器
{
公共函数构造()
{
$this->routes['GET']=[];
$this->路由['POST']=[];
}
公共函数句柄($request)
{
回声计数($this->routes['GET'])。“
”;//输出“0”…为什么??
}
受保护的函数addRoute($methods、$uri、$action)
{
#array_push($this->routes['GET'],'TEST');//也尝试过这个
$this->routes['GET'][]='test';
回声计数($this->routes['GET'])。“
”;//输出“1”
}
私人$routes=[];
}
执行addRoute
,然后使用handle($request)
中的count()
检查$this->routes[$methods]
的大小后,它指示0。已解决
啊,可怕的单身汉抓住了我。由于某种原因,没有使用同一对象。我已从应用程序中删除了单例模式。已解决
啊,可怕的单身汉抓住了我。由于某种原因,没有使用同一对象。我已经从我的应用程序中删除了单例模式。在
$methods
中有什么,您如何称呼它?您在哪里计数()?@colburton,已修复$方法在这里没有用处。我仍然看不到对addRoute()
或handle()
的任何调用。您很可能没有使用相同的对象。@colburton,请参阅下面我的答案。Singleton抓住了我…在$methods
中有什么,你怎么称呼它?您在哪里计数()?@colburton,已修复$方法在这里没有用处。我仍然看不到对addRoute()
或handle()
的任何调用。您很可能没有使用相同的对象。@colburton,请参阅下面我的答案。辛格尔顿让我。。。