Prefix all use of standard php classes with \ ?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
thinsoldier
Forum Contributor
Posts: 367
Joined: Fri Jul 20, 2007 11:29 am
Contact:

Prefix all use of standard php classes with \ ?

Post 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.
Warning: I have no idea what I'm talking about.
User avatar
Celauran
Moderator
Posts: 6425
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Prefix all use of standard php classes with \ ?

Post by Celauran »

Without spending much time having thought about it, couldn't you just add

Code: Select all

use DateTime;
if/when you start implementing namespaces?
thinsoldier
Forum Contributor
Posts: 367
Joined: Fri Jul 20, 2007 11:29 am
Contact:

Re: Prefix all use of standard php classes with \ ?

Post by thinsoldier »

Celauran wrote:Without spending much time having thought about it, couldn't you just add

Code: Select all

use DateTime;
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.
Warning: I have no idea what I'm talking about.
User avatar
Celauran
Moderator
Posts: 6425
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Prefix all use of standard php classes with \ ?

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13595
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Prefix all use of standard php classes with \ ?

Post by Christopher »

I guess I am wondering why you would spend several hours to prevent potential name clashes that will probably never occur?
(#10850)
thinsoldier
Forum Contributor
Posts: 367
Joined: Fri Jul 20, 2007 11:29 am
Contact:

Re: Prefix all use of standard php classes with \ ?

Post 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.
Warning: I have no idea what I'm talking about.
User avatar
Celauran
Moderator
Posts: 6425
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Prefix all use of standard php classes with \ ?

Post 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.
jkon
Forum Newbie
Posts: 16
Joined: Mon Feb 15, 2010 6:01 am

Re: Prefix all use of standard php classes with \ ?

Post 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
thinsoldier
Forum Contributor
Posts: 367
Joined: Fri Jul 20, 2007 11:29 am
Contact:

Re: Prefix all use of standard php classes with \ ?

Post 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/
Warning: I have no idea what I'm talking about.
User avatar
Christopher
Site Administrator
Posts: 13595
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Prefix all use of standard php classes with \ ?

Post 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.
(#10850)
jkon
Forum Newbie
Posts: 16
Joined: Mon Feb 15, 2010 6:01 am

Re: Prefix all use of standard php classes with \ ?

Post 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)
User avatar
Christopher
Site Administrator
Posts: 13595
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Prefix all use of standard php classes with \ ?

Post 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.
(#10850)
jkon
Forum Newbie
Posts: 16
Joined: Mon Feb 15, 2010 6:01 am

Re: Prefix all use of standard php classes with \ ?

Post by jkon »

Is there any reason to not prefix all uses of standard php classes with "\" for future namespace purposes?
Post Reply