Prefix all use of standard php classes with \ ?
Moderator: General Moderators
-
- Forum Contributor
- Posts: 367
- Joined: Fri Jul 20, 2007 11:29 am
- Contact:
Prefix all use of standard php classes with \ ?
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.
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.
Warning: I have no idea what I'm talking about.
Re: Prefix all use of standard php classes with \ ?
Without spending much time having thought about it, couldn't you just add
if/when you start implementing namespaces?
Code: Select all
use DateTime;
-
- Forum Contributor
- Posts: 367
- Joined: Fri Jul 20, 2007 11:29 am
- Contact:
Re: Prefix all use of standard php classes with \ ?
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.Celauran wrote:Without spending much time having thought about it, couldn't you just addif/when you start implementing namespaces?Code: Select all
use DateTime;
Warning: I have no idea what I'm talking about.
Re: Prefix all use of standard php classes with \ ?
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.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Prefix all use of standard php classes with \ ?
I guess I am wondering why you would spend several hours to prevent potential name clashes that will probably never occur?
(#10850)
-
- Forum Contributor
- Posts: 367
- Joined: Fri Jul 20, 2007 11:29 am
- Contact:
Re: Prefix all use of standard php classes with \ ?
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.Christopher wrote:I guess I am wondering why you would spend several hours to prevent potential name clashes that will probably never occur?
Warning: I have no idea what I'm talking about.
Re: Prefix all use of standard php classes with \ ?
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 \ ?
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
-
- Forum Contributor
- Posts: 367
- Joined: Fri Jul 20, 2007 11:29 am
- Contact:
Re: Prefix all use of standard php classes with \ ?
http://jason.pureconcepts.net/2013/04/p ... avoid-use/Celauran wrote:If you find it, please link it here. I'd be curious to read it.
Warning: I have no idea what I'm talking about.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Prefix all use of standard php classes with \ ?
The argument in that article is that "use can introduce name collisions and confusion." I think the argument can be true but is overstated.
(#10850)
Re: Prefix all use of standard php classes with \ ?
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)
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)
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Prefix all use of standard php classes with \ ?
The article was about the use statement, not the root namespace.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.
(#10850)
Re: Prefix all use of standard php classes with \ ?
Is there any reason to not prefix all uses of standard php classes with "\" for future namespace purposes?