V
V
Viktor Volkov2018-03-20 17:32:34
PHP
Viktor Volkov, 2018-03-20 17:32:34

How does writing a class name to a variable affect its namespace?

There is such a router, and if there is no necessary controller, the Error controller is loaded, the class of which is described in a separate file in the global namespace. But when the request matches one of the installed controllers, then it is called, respectively, it is also described in the global space (this is a background). So, when the name of the desired controller gets into $url, then it does not have "\" at the beginning. How does it happen that the controller class is still located??? (If you manually enter the contents of $url into the controller initialization line (remember, it will be there without a backslash), then there will be an error, and rightfully so!). The question is, how does writing a class name to a variable affect its namespace?

namespace app\controllers;
class Bootstrap
{
    public function __construct()
    {
        //echo 'бутстрап подключен<br>';
        $url = $_GET['url'];
        $url = rtrim($url, '/');
        $url = explode('/', $url);
        $file = 'app/controllers/'.$url[0].'.php';
       // echo $file.'<br>';
        if(file_exists($file)){

            require_once $file;
        } else {
            require_once 'app/controllers/error.php';
            $controller = new \Error;
            return false;
        }
            $url = ucfirst($url[0]);
            echo $url;
            $controller = new $url;

    }

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2018-03-20
@webinar

how does writing a class name to a variable affect its namespace?

no way. You do not have a namespace there, but a regular string.
What do you think this code does?
rtrim($url, '/');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question