1
I’m refactoring a small system in mvc that used the standard classmap
to the psr-4
, and was "obliged" to use multiple "use" within each controller to resolve the namespaces.
Example of what the classmap controller looked like:
use Particle\Filter\Filter; //biblioteca de filtro
use Rakit\Validation\Validator; // biblioteca de validacao de dados
class Teste extends Controller {
function index () {
$config = jsonConfig::get('dbconfig'); //classe qualquer
echo '<pre>';
print_r($config);
}
}
Using the same controller with psr-4:
namespace App\controllers;
use App\core\Controller;
use App\models\config\jsonConfig;
use Particle\Filter\Filter;
use Rakit\Validation\Validator;
class Teste extends Controller {
function index () {
$config = jsonConfig::get('dbconfig');
echo '<pre>';
print_r($config);
}
}
This is just an example, I have to do this in several controllers, models etc, there is some good practice to automate this, creating something like a Factory for controllers, for models etc, that already solves the generic namespaces of each "part" of the system.
Updating:
I also realized that I now need to use a backslash in calling native classes, e.g.: new PDO
turned new \PDO
That one
\
observation is to "exit" the current namespace. And what is the problem of severaluse
?– Costamilam
It doesn’t seem like a practical thing, having to keep making the hand N use s, I don’t see that in frameworks
– Thiago
What frameworks? Everyone I know who uses Composer needs to use the namespace. Possible is but I find it unfeasible. An alternative is to create nicknames for the class in Composer, it does not remove, but decreases the size
– Costamilam
Yeah, I know they do, I meant this long list of uses, it’s something more compact
– Thiago
In essence it is to make less complex codes. PHP is great for making simple code, but for some reason people have started to do more complex things than necessary. I think it’s because it’s fashionable. People don’t wonder what benefits it’s getting from complicating the code so much. What is not practical is to make MVC in simple codices. MVC has been created for extraordinarily complex applications.
– Maniero
I didn’t understand very well, I don’t think there is anything "complex" there, basically the principle of sole responsibility, so there are many USE
– Thiago
or it is better to continue with the classmap?
– Thiago