5
I would like to understand the great need and usefulness of namespaces in MVC architecture.
In what circumstances the use of namespaces becomes indispensable?
Give an example of a specific case if possible.
5
I would like to understand the great need and usefulness of namespaces in MVC architecture.
In what circumstances the use of namespaces becomes indispensable?
Give an example of a specific case if possible.
12
The main use of namespaces is for organization and to avoid collision of class names. PHP does not allow two classes to have the same name, and to avoid this problem we had to create very specific classes.
The motivator of this was the beginning of frameworks in PHP, which came as a proposal to solve common problems in development. Only this limited how to create our classes, because we could not repeat names. The solution was then to create very specific classes, prefixing with the name of your application.
Suppose you’re developing a class Service
to consult freight from a zip code.
Create a class called Service
may conflict with another class Service
for sending SMS.
So instead of doing this:
App_ConsultCepService
App_SendSmsService
With namespaces you can do something like this:
App\Cep\Service
App\Sms\Service
It also helps you to split your software into common interests, and also use autoloads already consolidated with major PHP projects, such as the PSR-4 that is implemented by Composer.
For more explanations see:
Browser other questions tagged php mvc namespace
You are not signed in. Login or sign up in order to post.
Related: PSR-4 in an MVC project or not?
– Woss
Dude, better than answering with text, I’ll give you a video. https://www.youtube.com/watch?v=AMwFTY1RLw
– Renato Paiva
@Andersoncarloswoss actually my doubt does not consist in the function of
namespace
but yes in critical cases where it becomes indispensable ... I do not intend to use simply as a modinha !– PululuK
But it is the functioning that defines the critical case: conflict of names.
– Woss
MVC has nothing to do with namespace.
– Daniel Omine