Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 多维关联数组_Php - Fatal编程技术网

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,请参阅下面我的答案。辛格尔顿让我。。。