If you haven't heard of Namespaces, or even PHP 5.3, you need to stop living under a rock. I've had quite a bit of fun the past month messing around with namespaces in 5.3, and I have got to say, they are the greatest addition to PHP yet. If you are unfamiliar with namespaces, it's a feature that allows you to add another layer of ownership and packaging to functions and classes, similar to how classes add a layer onto methods (ala functions).
In most applications and frameworks, you would see classes with very long names (I'm pointing the finger at you Zend!). This was implemented so that the classes would not conflict with internal and external (3rd party vendor) scripts. This is no longer the case, we can simply use namespaces! Lets take a look at some code we would write in 5.2 and lower.
class App_Utilities_Session {
public static function get($key) { }
public static function set($key, $value) { }
}
// Basically, annoying!
App_Utilities_Session::set('User', array());
You can see how this would get really annoying and time consuming having to write the long class names over and over again through out your application. We can now rewrite the code above using namespaces. Do note, that the namespace declaration has to be at the very top of your code; although comments and the declare() function can be above it.
namespace App\Utilities;
class Session {
public static function get($key) { }
public static function set($key, $value) { }
}
// Still kind of annoying
\App\Utilities\Session::set('User', array());
Now you may be thinking how this would make any difference, it's still a long class name to write... actually its even longer! Yeah well, namespaces also come bundled with aliases, if you don't feel like writing such long strings. Aliases allow you to declare alternative names for namespaces within the current scope (file). To apply an alias, you would write the "use" command with the Namespace path. You could either call the namespace by its base class name, or you can give it an alias. For example:
use App\Utilities\Session;
// Now that is much easier!
Session::get('user');
use App\Utilities\Session as S;
// Even easier!
S::get('user');
Even though namespaces are great and add extra functionality, there are a few things you should know about. Firstly, any class or function that isn't namespaced, or is within the global scope (SPL, Exception, etc), must be prepended with a backslash. If the backslash is not present, PHP will believe the class is within the current namespace and it fail.
$d = new \DirectoryIterator();
$e = new \Exception();
You would also need to prepend any global function. However, while developing I found that the backslash is not needed, unless you have a custom method with the same name of a function within the global scope.
class Debugger {
public static function log() { }
}
// Use the global log function
$o = \log();
Another benefit of using namespaces is when you structure your application and file paths to mirror the namespace names. For example, if I use the Session namespace above, I may have a folder structure like so.
// App\Utilities\Session
/app/utilities/session.php
This comes in handy when you have a custom function to convert a namespace into a file path, and vice versa. Furthermore, the get_class() method will now return the classname with the full namespace declaration.
$class = get_class($session);
// App\Utilities\Session
There are loads of implementations of features related to namespaces, I have only named off a few of the important ones. If you have not used 5.3 yet, I highly recommend you do so. Either by upgrading your server or installing a local server; my localhost of choice is XAMPP (it comes with 5.3 by default!). I also highly suggest looking at the namespace guide for more information.