Answer the question
In order to leave comments, you need to log in
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
how does writing a class name to a variable affect its namespace?
rtrim($url, '/');
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question