-1

The company I work for has requested that for an application (Laravel) we're developing to add controllers/models/repositories/validators to their own namespace (\Company\Whatever). I don't have a problem doing so but I'm curious as to what the benefits might be.

I would think that to do this I would simply autoload a custom name space (app/Company/Whatever) and create a new controller/model/whatnot directory structure under this.

Sturm
  • 4,105
  • 3
  • 24
  • 39
  • This question is probably going to be closed (not a good fit for this site), but namespaces have a few benefits: Less/no nameconflicts: each namespace can have a custom, extended `LogicException`, handled by a designated handler. Organized code == maintainable code. Namespaces enforce your project files to be stored consistently. Easier testing, better IDE autocompletion and warnings. In an namespaced project, each class starts with a series of `use Some\Namespace\Class` statements: at a glance, everyone knows what the dependencies of a class are – Elias Van Ootegem Jul 10 '14 at 14:25
  • Namespaces, in PHP at least, aren't as powerful/useful as they are in languages like C++, but they are important as your project grows. Just think back to those pre-namespace days, where classes were called `LibName_Service_Connection_Base`, now we write `Base`, if we're in the same namespace, or `use LibName\Service\Connection\Base;` once, and use `Base` throughout the code... do not underestimate laziness, it's a factor – Elias Van Ootegem Jul 10 '14 at 14:28
  • @EliasVanOotegem any particular SE site that this would be a good ask on? – Sturm Jul 10 '14 at 14:39
  • webapplications possibly, but there are some napespace-related questions on [programmers](http://programmers.stackexchange.com/) already: http://programmers.stackexchange.com/questions/221524/pros-and-cons-of-namespaces-vs-include-require-in-php seems to answer this question for you, voting to close – Elias Van Ootegem Jul 10 '14 at 14:44
  • 1
    Not a good fit, [question exists on programmers](http://programmers.stackexchange.com/questions/221524/pros-and-cons-of-namespaces-vs-include-require-in-php) – Elias Van Ootegem Jul 10 '14 at 14:46
  • If your company uses a lot of 3rd party libraries, this would be a very good idea as there wouldn't be much of a chance of having any conflicts with class names. – user1669496 Jul 10 '14 at 14:59
  • We're not using all that many - not any that I would see as a potential conflict (ie: Ardent). We do have "events" which will conflict with Laravel's Event class, so there's that I suppose. – Sturm Jul 10 '14 at 15:01
  • Possible duplicate of [What is the difference between PSR-0 and PSR-4?](http://stackoverflow.com/questions/24868586/what-is-the-difference-between-psr-0-and-psr-4) – Sturm Jan 06 '16 at 22:02

1 Answers1

1

There are a few benefits to doing this.

1.) PSR-0/PSR-4 Autoloading

If you have a vendor namespace, then you can use PSR-0 or PSR-4 which makes autoloading insanely easy. You can just put this in your composer.json:

"autoload": {
    "psr-4": {
        "App\\": "app/src/"
    }
},

2.) Collision Avoidance

Theoretically your code should be ok hogging the global namespace as everything else should be in its own namespace, but this is PHP and a) PHP itself doesn't use namespaces for any core classes b) not everyone out there is distributing their code under a namespace either. In 2014 this is much much much better than it was in 2010, but it is still potentially an issue.

There are probably some other reasons, but these two do it for me.

Phil Sturgeon
  • 30,637
  • 12
  • 78
  • 117