PHP Developers Network
http://forums.devnetwork.net/

Prefix all use of standard php classes with \ ?
http://forums.devnetwork.net/viewtopic.php?f=19&t=142494
Page 1 of 1

Author:  thinsoldier [ Thu Apr 21, 2016 9:00 am ]
Post subject:  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.

Author:  Celauran [ Thu Apr 21, 2016 9:12 am ]
Post subject:  Re: Prefix all use of standard php classes with \ ?

Without spending much time having thought about it, couldn't you just add
Syntax: [ Download ] [ Hide ]
use DateTime;

if/when you start implementing namespaces?

Author:  thinsoldier [ Thu Apr 21, 2016 9:56 am ]
Post subject:  Re: Prefix all use of standard php classes with \ ?


Author:  Celauran [ Thu Apr 21, 2016 10:04 am ]
Post subject:  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.

Author:  Christopher [ Thu Apr 21, 2016 1:52 pm ]
Post subject:  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?

Author:  thinsoldier [ Fri Apr 22, 2016 1:42 pm ]
Post subject:  Re: Prefix all use of standard php classes with \ ?


Author:  Celauran [ Fri Apr 22, 2016 2:21 pm ]
Post subject:  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 statements because having to inline \Symfony\Component\HttpFoundation\Request et al. all over the place sounds horrible.

Author:  jkon [ Sun Jun 05, 2016 7:59 pm ]
Post subject:  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

Author:  thinsoldier [ Mon Jun 06, 2016 11:45 pm ]
Post subject:  Re: Prefix all use of standard php classes with \ ?


Author:  Christopher [ Tue Jun 07, 2016 4:54 pm ]
Post subject:  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.

Author:  jkon [ Sat Jun 11, 2016 12:59 am ]
Post subject:  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)

Author:  Christopher [ Sat Jun 11, 2016 11:08 am ]
Post subject:  Re: Prefix all use of standard php classes with \ ?


Author:  jkon [ Sat Jun 11, 2016 1:12 pm ]
Post subject:  Re: Prefix all use of standard php classes with \ ?


Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/