PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sun Oct 20, 2019 12:47 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 13 posts ] 
Author Message
PostPosted: Thu Apr 21, 2016 9:00 am 
Offline
Forum Contributor

Joined: Fri Jul 20, 2007 11:29 am
Posts: 367
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.


Top
 Profile  
 
PostPosted: Thu Apr 21, 2016 9:12 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada
Without spending much time having thought about it, couldn't you just add
Syntax: [ Download ] [ Hide ]
use DateTime;

if/when you start implementing namespaces?

_________________


Top
 Profile  
 
PostPosted: Thu Apr 21, 2016 9:56 am 
Offline
Forum Contributor

Joined: Fri Jul 20, 2007 11:29 am
Posts: 367

_________________
Warning: I have no idea what I'm talking about.


Top
 Profile  
 
PostPosted: Thu Apr 21, 2016 10:04 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada
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.

_________________


Top
 Profile  
 
PostPosted: Thu Apr 21, 2016 1:52 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US
I guess I am wondering why you would spend several hours to prevent potential name clashes that will probably never occur?

_________________
(#10850)


Top
 Profile  
 
PostPosted: Fri Apr 22, 2016 1:42 pm 
Offline
Forum Contributor

Joined: Fri Jul 20, 2007 11:29 am
Posts: 367

_________________
Warning: I have no idea what I'm talking about.


Top
 Profile  
 
PostPosted: Fri Apr 22, 2016 2:21 pm 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada
If you find it, please link it here. I'd be curious to read it. Meanwhile, I'm going to keep using statements because having to inline \Symfony\Component\HttpFoundation\Request et al. all over the place sounds horrible.

_________________


Top
 Profile  
 
PostPosted: Sun Jun 05, 2016 7:59 pm 
Offline
Forum Newbie

Joined: Mon Feb 15, 2010 7:01 am
Posts: 16
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


Top
 Profile  
 
PostPosted: Mon Jun 06, 2016 11:45 pm 
Offline
Forum Contributor

Joined: Fri Jul 20, 2007 11:29 am
Posts: 367

_________________
Warning: I have no idea what I'm talking about.


Top
 Profile  
 
PostPosted: Tue Jun 07, 2016 4:54 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US
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)


Top
 Profile  
 
PostPosted: Sat Jun 11, 2016 12:59 am 
Offline
Forum Newbie

Joined: Mon Feb 15, 2010 7:01 am
Posts: 16
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)


Top
 Profile  
 
PostPosted: Sat Jun 11, 2016 11:08 am 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US

_________________
(#10850)


Top
 Profile  
 
PostPosted: Sat Jun 11, 2016 1:12 pm 
Offline
Forum Newbie

Joined: Mon Feb 15, 2010 7:01 am
Posts: 16


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group