What is the reasoning behind requireing namespacing?

Silverstripe Version:
SS 4x
Question:

I have been using SilverStripe since version 2 was released. One of the reasons I chose this as my preferred platform was because I did not have to put require, use, include statements at the beginning of each file like has to be done for C++, .NET, Action Script, etc. I really liked that I could just type the name of a class and it was autoloaded. It seems like a big step backwards to now have to use such mechanisms. I understand that it is a bit more efficient but SS 3 is not slow for basic websites and even pretty complex web applications. I have now built over 150 websites in SS and have been very happy. I will continue to use SS but am not happy to have to spend a lot more time figuring out the “use” statements for every class I want to use. So what I really want to know is how are namespaces justified when put against the amount of time it will add to each project I start building.

Thanks for any input in helping my understanding.

1 Like

This isn’t really a Silverstripe-specific issue. Namespacing is a part of modern PHP development, and has been for a while now. It allows for proper separation of codebases and helps to avoid collisions with class names, etc. This can be seen even within the available Silverstripe add-ons where (pre-namespacing) authors had to be very careful not to cause collisions with core code and other add-ons.

If you only ever use Silverstripe in isolation, then the chances of collisions are less, but if you want to use it in an environment which supports proper autoloading, or use it alongside other codebases then Namespacing becomes pretty much essential. I’ve actually had to avoid Silverstripe in the past because it didn’t support Namespaces properly and that caused all kinds of compatibility issues.

Most modern IDEs should be able to perform the relevant class name look-ups to help with the use statements. It does take a little extra time, but as I say, it’s a part of PHP development nowadays.

Just as a follow-up. The use statement in PHP is different from include and require. The latter are manual instructions to the parser to load additional files.
Namespaces are a different beast, the use statement doesn’t directly instruct PHP to load the file, instead it tells PHP that you want to use something (classes, class methods, etc.) from a different namespace. The autoloaders then do their stuff and load the relevant files.

Similar to you, I have been using SS since 2.x and have been “pampered” by that convenience of not requiring namespace.

But it is really a modern feature designed to enable sensible scalability.

Oftentimes I have encountered in SS3 cryptic errors that is not easily debuggable until that “aha” moment of class name clashing.

Like in a website where you feature “Director” (the movie occupation), you are forced to rename the class to “MovieDirector”, simply because some class in SS is already named “Director”

Similarly with SS going into a modular/dependency model, you will find increasingly more classes named the same. Who is to say “Image” file asset is more correct a name than say “Image” for a database snapshot?

With namespacing these problems go away.

A tip for your gripe about having to figure out “use” statements, some IDEs (I am using Visual Studio Code) offer easy import features, so you can just right-click on the class to import the class; this has made figuring out the namespaces incredibly easy.

Remember when SilverStripe started with composer? It broke my well used habbit. Caused a lot of WTFs, but now I love it.

which is a de-facto namespace…

Proper PSR-4 namespacing means also faster autoloading.

@micahsheets A proper IDE like PHPStorm, Netbeans or VSCode will help you adding the “use” statements. In the beginning it’s a bit hard to get used to namespacing, but after some projects you’ll have your own systen. IMHO you should use some IDE; it’s more than a plain editor, and has it tries to understand your code it can help you more. One feature I like most with PHPStorm is the “jump” to a class. So it doesn’t matter where a class is located in the filesystem. Reading (and understanding) more source code also makes you a better developer.

I once wanted to add a PDF library that was not namespaced and had a class “Tag”… but my project already had a DataObject “Tag”. If SilverStripe allowed namespacing dataobjects at that time I’d have been very happy. So I had to move to the unstable next release of that lib that was happily namespaced.

TLDR;
Take this chance that SilverStripes wants you to namespace your code to become better.

Than kyou for your input. I use PHPStorm so I can see about taking advantage of it’s features.

1 Like

In that case, you can even set it up to automatically add namespaces on save (when there is only one option). :slight_smile:

Not sure what setting I have but it tends to autocomplete my namespaces - and if not, you can hit alt + enter (on Windows, not sure on Mac OS etc) and Import class... to bring it in for you. You’ll get used to it :slight_smile:

The place it most often catches me is in yaml files - but once namespaces become second nature, it becomes odd to see a class in a yaml file without at least one \ :joy:

1 Like

BTW: did anyone get SilverStripe’s upgrader doing upgrades to yml files? That feature should be there according to their docs, but it doesn’t work over here.