Page 1 of 1
Prefix all use of standard php classes with \ ?
Posted: Thu Apr 21, 2016 9:00 am
by thinsoldier
Is there any reason to not prefix all uses of standard php classes with "\" for future namespace purposes?
I have a massive old project that doesn't use namespaces but a few years from now might be refactored heavily and when that happens I'll probably use namespaces.
I have many small recent projects that will eventually get large sections of the old project copy/pasted into them over the coming years as clients realize 1 by 1 that they did actually need all those features they didn't want to pay for in the beginning.
I intend to use proper namespaces in my next small project.
Should I take a few hours to go through all the code in the old project and prefix uses of classes like: \DateTime, \stdClass, \ErrorException ? ...Actually those might be the only 3 I've used in there.
Re: Prefix all use of standard php classes with \ ?
Posted: Thu Apr 21, 2016 9:12 am
by Celauran
Without spending much time having thought about it, couldn't you just add
if/when you start implementing namespaces?
Re: Prefix all use of standard php classes with \ ?
Posted: Thu Apr 21, 2016 9:56 am
by thinsoldier
Celauran wrote:Without spending much time having thought about it, couldn't you just add
if/when you start implementing namespaces?
It seem you have to go into every individual file and add a use statment. I can't just add use DataTime to my boot/config.php file and have it work. So I figured it might be worth the explicit specificity to just prefix each instance with the "\" that signifies "php's root namespace" since I'd have to make a change in every relevant file anyway.
Re: Prefix all use of standard php classes with \ ?
Posted: Thu Apr 21, 2016 10:04 am
by Celauran
True, I was just thinking you'd need to make it once per file if/when you needed it versus doing it once per call now. Either will work, though.
Re: Prefix all use of standard php classes with \ ?
Posted: Thu Apr 21, 2016 1:52 pm
by Christopher
I guess I am wondering why you would spend several hours to prevent potential name clashes that will probably never occur?
Re: Prefix all use of standard php classes with \ ?
Posted: Fri Apr 22, 2016 1:42 pm
by thinsoldier
Christopher wrote:I guess I am wondering why you would spend several hours to prevent potential name clashes that will probably never occur?
As an experiment I copied some files from one site to another where I was experimenting with namespacing and nothing worked due to not being able to find stdclass and datetime and after fixing it multiple ways I found the easiest/laziest way was to just either use a use statement in the relevant files or prefix the class uses with \ and for some reason I "feel" like I prefer the explicitness of adding the \ over use statments. I also may have read an article a while back advocating for not using the use statement at all, but I don't remember its reasonong.
Re: Prefix all use of standard php classes with \ ?
Posted: Fri Apr 22, 2016 2:21 pm
by Celauran
If you find it, please link it here. I'd be curious to read it. Meanwhile, I'm going to keep using use statements because having to inline \Symfony\Component\HttpFoundation\Request et al. all over the place sounds horrible.
Re: Prefix all use of standard php classes with \ ?
Posted: Sun Jun 05, 2016 7:59 pm
by jkon
Namespaces in PHP is one of the most misunderstand and misused feature. There are many reasons behind that , but it has a meaning if you are going to use namespaces at all first understand why are there , how do they really work , from the PHP manual first. Then if you really understand those you will have your own reflexes out of nonsenses as “namespaces are used for autoloading” or anything like that
Re: Prefix all use of standard php classes with \ ?
Posted: Mon Jun 06, 2016 11:45 pm
by thinsoldier
Celauran wrote:If you find it, please link it here. I'd be curious to read it.
http://jason.pureconcepts.net/2013/04/p ... avoid-use/
Re: Prefix all use of standard php classes with \ ?
Posted: Tue Jun 07, 2016 4:54 pm
by Christopher
The argument in that article is that "use can introduce name collisions and confusion." I think the argument can be true but is overstated.
Re: Prefix all use of standard php classes with \ ?
Posted: Sat Jun 11, 2016 12:59 am
by jkon
I can't see how not using the root namespace sign “\” inside the root namespace can introduce name collisions and confusion, except if someone is using PHP namespaces in ways that are not introduced for and I can't imagine right now.
Lets say you have a class StringUtils in the root and then there is a module/or anything like that with namespace “Something” that has a class StringUtils in its root. When you are inside that module/or anything when you type StringUtils PHP understands the StringUtils that is in that namespace. But even inside that namespace if you write \StringUtils PHP will understand the StringUtils of root (although that has rarely any point because if you are in a module it is agnostic or nearly agnostic of the root namespace).
If you are in root namespace you couldn't have a second class named StringUtils under it in any way. So what exactly are you avoiding using the root namespace sign inside the root ?
(as I have wrote many times PHP namespaces is one of those language features that are most misused , probably due to lack of OOP understanding)
Re: Prefix all use of standard php classes with \ ?
Posted: Sat Jun 11, 2016 11:08 am
by Christopher
jkon wrote:I can't see how not using the root namespace sign “\” inside the root namespace can introduce name collisions and confusion, except if someone is using PHP namespaces in ways that are not introduced for and I can't imagine right now.
The article was about the
use statement, not the root namespace.
Re: Prefix all use of standard php classes with \ ?
Posted: Sat Jun 11, 2016 1:12 pm
by jkon
Is there any reason to not prefix all uses of standard php classes with "\" for future namespace purposes?