Why should i (not) use the “use” keyword for namespaces
I've been working with PHP 5.3 for more than a year now, and there's something I always do that I'm not sure is correct : whenever I use a class outside of the current context's namespace, is use the use keyword :
so in the code I can write directly :
$object = new Class();
That's fine, but I often end up with a lot of use directives at the beginning of my files, and that's not so nice to maintain (if I don't use this class, I'm not likely to remove the use directive).
I could also write the whole namespace each time I use this class :
$object = new \Another\Class();
So I was wondering two things :
- Is there any drawback of not using the use keyword (except the fact that you have to write the whole namespace each time), like things you can't do ?
- Is there any drawback of using it ? (is it even slightly less performant because it somehow imports the class) ?
Edit Just to clarify : I know clean code is more important than small performance tweaks, I just like to know when the computer is working, I like to feel the efforts :-)
First thing that comes in my mind is the ability to use aliases like:
use My\Full\Classname as Another;
source php manual
The one big drawback to not using use, is the need to type out the full name every time.
The one big drawback to using it, is the possibility of name collisions with classes that are already defined. I'm not sure how PHP handles that, but either way you end up with a little extra hassle. The plus is that you don't actually have to use the same name...but then, the minus to that is that you have classes that don't correspond to filenames.
Before we even get into more of this performance BS, though: If you care about the performance cost of aliasing a freaking name, you're doing it wrong. Do not worry about "faster" syntax. Period. You, your users, and that guy compulsively clicking "Reload" with a stopwatch in hand, will not notice the difference. Ever. There are far better things to optimize, and that's assuming you've reached the point where you need to.
Namespaces are not backward compatible with PHP < 5.3 you will have to remove all cases of namespace and use statements if you come across a compatibility issue.
I use Eclipse with PDT and the capability of Eclipse to complete syntax, both using use or complete namespace, make it easy.
Personally, I use "use" most the time (that Eclipse creates automatically when you use autocomplete of classnames), because make a cleaner syntax on the code.
I use a ClassLoader too, and my project use folders in the same order than namespace, so my classloader loads directly each class. I don't need any "include" instruction.
I always use classes (with fex exceptions), even for normal code, where normally i use static code (without instances), because it makes very easy to Eclipse to recognice each class, methos, variable and associated type.
So my code is a lot of files, without global variables, and separated modules on classes so class name collision is rare. And in this few cases, where is easy with Eclipse to detect, I use the complete syntax, or abbreviates (manually, I use an alias of the namespace so I use "namespace_alias\classname" syntax, that is cleaner but don't make collisions).
Use namespace and files that use the same structure and a classloader that search in this way (and adds all paths on include_path directory) has been very productive on large projects.
For example... \Example\Name\Space\Myclass search as
for each listoffolders (root of the project + all in include_path except ".") On the start folder -> folder (Example) -> Folder (Name) -> Folder (Space) -> File (Myclass.inc or Myclass.php or Myclass.php5) endfor