2017-07-09 22 views
1

なぜこれをプッシュバックまたはエンドに追加しないのですか?配列の多次元連想配列

私は両方の方法を試しました。

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 = []; 
} 

私はaddRouteを実行し、その後handle($request)count()を使用して$this->routes[$methods]のサイズを確認した後、それが解決0

+1

'$ methods'には何がありますか、これはどのように呼びますか?あなたはどこで 'count()'しますか? – colburton

+0

@colburton、fixed。 $メソッドはここでは役に立たない。 – keelerjr12

+1

'addRoute()'や 'handle()'はまだ呼び出せません。同じオブジェクトを使用していない可能性が最も高いです。 – colburton

答えて

0

を示して...

ああ、恐ろしいシングルトンは私を得ました。何らかの理由で、同じオブジェクトが使用されていませんでした。私はアプリケーションからシングルトンパターンを削除しました。