Wednesday, May 12, 2010

How do you like to "use" it?

According to the PHP.net manual "PHP namespaces support two kinds of aliasing or importing: aliasing a class name, and aliasing a namespace name". So basically this leaves two options for the average case:



Aliasing a namespace name



namespace ns1\ns11\ns111 {
class c1 { }
}
namespace ns2\n21 {
use \ns1\ns11\ns111;
$v = new ns111\c1();
}

Aliasing a class name



namespace ns1\ns11\ns111 {
class c1 { }
}
namespace ns2\n21 {
use \ns1\ns11\ns111\c1;
$v = new c1();
}

While both options are useful developers will probably prefer to use the one over the other in different cases. For example if your script imports many classes declared in one namespace, only one namespace can be imported avoiding the need to import each class separately.

Option A:

use \ns1\ns11\ns111\c1;
use \ns1\ns11\ns111\c2;
use \ns1\ns11\ns111\c3;
use \ns1\ns11\ns111\c4;
use \ns1\ns11\ns111\c5;

$v = new c1(new c2(new c3(new c4(new c5()))))


Option B:

use \ns1\ns11\ns111;
$v = new ns111\c1(new ns111\c2(new ns111\c3(new ns111\c4(new ns111\c5()))))

How Eclipse PDT should complete your "use" statement?


A real cool feature in Eclipse PDT is that use statements are completed automatically for you. The question is what method should be considered:
1. Aliasing a class name
2. Aliasing a namespace name
3. Depends on the case, what are the different cases?

Vote here: http://twtpoll.com/omuaqn


1 comment:

Jacek Pospychala said...

option A is much more readable and faster to write.