Did you know? DZone has great portals for Python, Cloud, NoSQL, and HTML5!

Lorna Jane Mitchell is a PHP developer, blogger, trainer and evangelist from Leeds in the UK. She is active with phpwomen.org and her local user group PHP North West, and writes for a variety of outlets, including her own blog at lornajane.net. She is an active member of the PHP and open source communities and contributes to the joind.in event feedback project. When she's not at her computer, Lorna enjoys yarn craft, hobby electronics, and her home renovation project. Lorna is a DZone MVB and is not an employee of DZone and has posted 33 posts at DZone. You can read more from them at their website. View Full User Profile

3 Ways to Access a Namespaced PHP Class

02.03.2011
Email
Views: 3866
  • submit to reddit
After what felt like years of debate over the notation to use for PHP's namespaces, it seems like the feature itself has had relatively little use or attention since it was actually implemented in PHP 5.3. We're all used to working without it but using it does make code neater.

Take this example (in a file called namespaced-class.php)
namespace Christmas\DaysOf;  

class PartridgeInAPearTree{
}
Now we have a few ways to access that class.

Refer Namespace and Class Name


The simplest way to access a class inside a namespace is simply to prefix the classname with its namespace(s):

include 'namespaced-class.php';

$bird1 = new Christmas\DaysOf\PartridgeInAPearTree();
var_dump($bird1); 
You can use this anywhere in your code.

Import the Namespace


In PHP we can import namespaces using the use keyword, and then just use the last part of the name in place of the namespace. In my contrived Christmas-related example, we'd end up with something that looks like this:
include 'namespaced-class.php';
use Christmas\DaysOf;

$bird2 = new DaysOf\PartridgeInAPearTree();
var_dump($bird2);
This example works well because it saves us from typing anything but the last element of a potentially quite long-winded path.

Alias the Namespace and/or Class


We can alias just the namespace, as shown above, or right down to the class name, using the use keyword. Optionally we can also rename what we'd like to call it, rather than the final element in the path, by using the as keyword.
include 'namespaced-class.php';
use Christmas\DaysOf\PartridgeInAPearTree as Bird;            

$bird3 = new Bird();
var_dump($bird3);
 
Even better, we can give a "nickname" to our namespace, either to avoid clashes or just to give us something more memorable or descriptive to use within the class. This will also be a great trick to use when namespace-using applications become legacy and we have to handle renaming of namespaces.

So Many Options


All of the above examples give a debug output of the same class; the class that was declared in the opening example file and included elsewhere. One thing I noted which was unfamiliar is that you must declare a namespace as the first thing in a file, and that the namespace is implicitly ended at the end of the file.

Are you using namespaces in your applications? How have you implemented them? I'm interested to hear how others are applying these techniques inside their architectures, please share!



References
Tags:
Published at DZone with permission of Lorna Mitchell, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Markus Lanz replied on Fri, 2011/02/04 - 4:22am

Generally I am Importing not just the namespace, also the class and use it like that

use Foo\Example\Class;
$bla = new Class();


When I get into a nameconflict, I prefer the full qualified namespace instead an alias. (git grep, find, etc...)
use Foo\Example\Class;
// use Great\Lib\From\Github\Class; // conflict
$bla = new Great\Lib\From\Github\Class();

Thats the way I work with namespaces.

With best regards and see you in Manchester 19th/20th
Markus

PS: Would be great to hear about "Speaking Tips"

Rehman Khan replied on Sat, 2012/02/25 - 6:46am

I'm using namespaces and am loving them. However there are still a few things I don't like in how they're implemented.

My most recent discovery is about callbacks. Simply speaking, you can't use aliased classname in a callback. You need to pass a fully qualified name.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.