1

I am just starting with php and I can't create this object.

I call my object with the variable $theClass is a concatenation of a namespace and a variable from a an array. One var_dump of $theclass show me the right path... the problem start wen I try to create a new $theClass on variable($control) the var_dump show me nothing...

<?php

namespace App\Router;

class Router {
    private $routes = [];
    private $url;

    public function __construct($url){

        $this->url = $url;

    }

    public function get($path, $action){

        $this->routes['GET'][$path] = $action;
    }

    public function match(){

        foreach ($this->routes as $key => $routes) {
            foreach ($routes as $path => $action) {
                if ($this->url === $path) {
                    $elements = explode('@', $action);
                    //$this->callController($elements);
                    $theClass = "App\Controller\\$elements[0]";
                    // I try this way to 'App\Controller\\' . $elements[0];
                    var_dump($theClass);
                    $method = $elements[1];
                    var_dump($method);
                    $control = new $theClass();
                    $control->$method();
                }
            }
            header('HTTP/1.0 404 Not Found');
        }
    }
}
Mickaël Leger
  • 3,426
  • 2
  • 17
  • 36
rafinha187
  • 77
  • 1
  • 8
  • Try put another \ between `App` and `Controller` and remove the `()` from the instancing command. See https://stackoverflow.com/a/30647705/6487675 – dWinder Nov 20 '19 at 10:52
  • So what does your http serer's error log file reveal what the issue is? – arkascha Nov 20 '19 at 11:09

0 Answers0