Is it OK to use purely static class as a namespace?
Moderator: General Moderators
- kaisellgren
- DevNet Resident
- Posts: 1675
- Joined: Sat Jan 07, 2006 5:52 am
- Location: Lahti, Finland.
Is it OK to use purely static class as a namespace?
Hi,
As the title says, is it oll korrect to use a class, which is entirely static, to serve its purpose as a namespace?
Namespaces are only available as of PHP 5.3+ and I am requiring PHP 5.2+ to correctly run my application. So, maybe in the future (I always think about the future), I might require PHP 5.3+, thus, I could use namespaces as well. However, if my entire application is written without those double colons, I would need to change plenty of code prior to using namespaces. So, would it be acceptable to use static methods in a class to construct a "namespace"?
As the title says, is it oll korrect to use a class, which is entirely static, to serve its purpose as a namespace?
Namespaces are only available as of PHP 5.3+ and I am requiring PHP 5.2+ to correctly run my application. So, maybe in the future (I always think about the future), I might require PHP 5.3+, thus, I could use namespaces as well. However, if my entire application is written without those double colons, I would need to change plenty of code prior to using namespaces. So, would it be acceptable to use static methods in a class to construct a "namespace"?
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Is it OK to use purely static class as a namespace?
Can I just say NO and that's the end of it? 
I would recommend using PEAR naming until then.
I would recommend using PEAR naming until then.
(#10850)
Re: Is it OK to use purely static class as a namespace?
Why, sure. But instead of using RandomName::MyFunction1, RandomName::MyFunction2, etc, is there an actual disadvantage to just calling it RandomName_MyFunction1, RandomName_MyFunction2, etc? Other than having a non-OOP'ish feel about it (which probably offends certain people) there's no real difference.kaisellgren wrote:So, would it be acceptable to use static methods in a class to construct a "namespace"?
- kaisellgren
- DevNet Resident
- Posts: 1675
- Joined: Sat Jan 07, 2006 5:52 am
- Location: Lahti, Finland.
Re: Is it OK to use purely static class as a namespace?
Yes. For instance, if I do this:Apollo wrote:is there an actual disadvantage to just calling it RandomName_MyFunction1, RandomName_MyFunction2, etc?
Code: Select all
RandomName::MyFunction();- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Is it OK to use purely static class as a namespace?
Why not? It's your autoload code.kaisellgren wrote:It will automatically load the correct library (spl auto loading), but with your RandomName_MyFunction(), unfortunately, that will not happen.
(#10850)
- kaisellgren
- DevNet Resident
- Posts: 1675
- Joined: Sat Jan 07, 2006 5:52 am
- Location: Lahti, Finland.
Re: Is it OK to use purely static class as a namespace?
How? After I make the initial call to RandomName_MyFunction() I will get an undefined function error.arborint wrote:Why not? It's your autoload code.kaisellgren wrote:It will automatically load the correct library (spl auto loading), but with your RandomName_MyFunction(), unfortunately, that will not happen.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Re: Is it OK to use purely static class as a namespace?
Yep, you can't autoload functions. Only classes.arborint wrote:Why not? It's your autoload code.kaisellgren wrote:It will automatically load the correct library (spl auto loading), but with your RandomName_MyFunction(), unfortunately, that will not happen.
Re: Is it OK to use purely static class as a namespace?
Ah yes, you're right. I stand corrected sirkaisellgren wrote:It will automatically load the correct library (spl auto loading), but with your RandomName_MyFunction(), unfortunately, that will not happen.
I'm not a big fan of autoloading myself, in the very few times I've used it, somehow I soon enough found myself disabling it and manually (explicitly) controlling exactly which files were included and which weren't. Especially when debugging, I sometimes quickly want to isolate a piece of code, and not have random other files being included behind the scenes. Maybe it's got to do with me being a C++ purist
Re: Is it OK to use purely static class as a namespace?
What problems do you experience with irrelevant code being included when debugging? Are you using globals?Apollo wrote:I'm not a big fan of autoloading myself, in the very few times I've used it, somehow I soon enough found myself disabling it and manually (explicitly) controlling exactly which files were included and which weren't. Especially when debugging, I sometimes quickly want to isolate a piece of code
Re: Is it OK to use purely static class as a namespace?
Sometimes, yes (because imho it's useless to enforce an OOP approach for things that are really just static / global).astions wrote:What problems do you experience with irrelevant code being included when debugging? Are you using globals?
But especially when pinpointing a problem or isolating a certain piece of code, I'd like to be able to strip off as much as possible.
- kaisellgren
- DevNet Resident
- Posts: 1675
- Joined: Sat Jan 07, 2006 5:52 am
- Location: Lahti, Finland.
Re: Is it OK to use purely static class as a namespace?
NuSphere PhpEd has a nice debugger. If you have not tried it yet, you should give it a shot.
The autoloading is something that I would like to keep in my application. Not that I am a lazy programmer, but it does make things easier for other developers working on my project and the fact that it is lazy loading classes, it could improve performance, too - esp. with "bad" developers, who work on my project.
Anyway, so, do you guys allow me to use classes for namespaces ?
The autoloading is something that I would like to keep in my application. Not that I am a lazy programmer, but it does make things easier for other developers working on my project and the fact that it is lazy loading classes, it could improve performance, too - esp. with "bad" developers, who work on my project.
Anyway, so, do you guys allow me to use classes for namespaces ?
Re: Is it OK to use purely static class as a namespace?
No objectives herekaisellgren wrote:Anyway, so, do you guys allow me to use classes for namespaces ?