One of the major issues that Object Oriented design addresses is the problem of having tons of functions and variables all in the same global name space. By modularizing the code you don’t run into the problem of having obnoxiously long function and variable names. More modularized code means it’s far easier to reuse later on different projects without conflict.
While that might seem like the end of the story, it isn’t be any means. As a project grows and starts into using elements of other projects you suddenly find yourself with a whole new naming problem. Now those classes are starting into getting those obnoxiously long names. Other OOP languages that are more established in this realm, like Java and Python, have implemented solutions to these problems long ago. It seems that PHP is finally starting to address this.
I personally didn’t think we would see PHP release this until version 6, but it looks like as of PHP 5.3.0 you can start into working with defining namespaces. To quote the PHP manual…
To better get a handle on what this is all about comes from a post by Dmitry Stogov to the PHP Internals mailing list.
The full thread is well worth the read since these were many of the formative arguments that have lead to what is in there today, and what will likely be where things are at when the dust settles.
As interesting as all of this is, namespaces are still not a part of the the stable PHP release just yet. It’s very likely that it will be months even after 5.3.0 hits the streets before web hosts are fully upgraded. There are also a number of unresolved issues facing the PHP developers. One such problem is noted in a post on the PHP manual concerning the namespace rules.
Sadly enough, the latest class name resolution rules render PHP namespaces quite useless.
Let’s face it, not only do us users of PHP have namespace issues, the language itself is getting out of control. I can remember back when this list would nearly fit on a single page with two columns. Everything on there exists in the global namespace. As PHP continues to grow and add functionality are we forever going to see the function list grow?
Java dealt with this problem early on by keeping the core functionality of the language to a bare minimum, then distributing libraries in the form of packages that each exist within a namespace. It may be that with the combination of a more complete object model and the need to add core functionality PHP may be forced into that type of model. If a packages style model is in the works, it’s most likely out there on the fringes of a PHP7 wish list that I haven’t seen yet.
My first immediate concern as I got looking into this was how autoloading would be impacted. I use autoloading for all my class imports, so this is near and dear to my heart. Searching about I found the following post discussing this. The interesting stuff is in the follow up replies, so be sure to check out the full thread. It’s only a couple of messages.
[PHP-DEV] PHP 5.3 Autoload+Namespaces+Functions incorrect (atleast weird) behavior.
I then ran across a blog entry by David Coallier discussing namespaces and their usage. Within the comments he addresses autoloading. Many of the other comments express a pretty negative set of opinions about what Dmitry has worked up here.
Whatever the final direction of this goes, it’s important to keep in mind that PHP started as a fairly simple procedural scripting language. What we’re seeing now are the growing pains associated with becoming a platform that can readily handle much larger projects than would be practical today. Look for all of this goodness to be hitting mirrors near you very soon.