Page 1 of 1
Is it OK to use purely static class as a namespace?
Posted: Thu Apr 02, 2009 1:23 pm
by kaisellgren
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"?
Re: Is it OK to use purely static class as a namespace?
Posted: Thu Apr 02, 2009 2:43 pm
by Christopher
Can I just say NO and that's the end of it?
I would recommend using PEAR naming until then.
Re: Is it OK to use purely static class as a namespace?
Posted: Thu Apr 02, 2009 3:03 pm
by Apollo
kaisellgren wrote:So, would it be acceptable to use static methods in a class to construct 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.
Re: Is it OK to use purely static class as a namespace?
Posted: Thu Apr 02, 2009 3:16 pm
by kaisellgren
Apollo wrote:is there an actual disadvantage to just calling it RandomName_MyFunction1, RandomName_MyFunction2, etc?
Yes. For instance, if I do this:
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?
Posted: Thu Apr 02, 2009 4:14 pm
by Christopher
kaisellgren wrote:It will automatically load the correct library (spl auto loading), but with your RandomName_MyFunction(), unfortunately, that will not happen.
Why not? It's your autoload code.
Re: Is it OK to use purely static class as a namespace?
Posted: Thu Apr 02, 2009 4:21 pm
by kaisellgren
arborint wrote:kaisellgren wrote:It will automatically load the correct library (spl auto loading), but with your RandomName_MyFunction(), unfortunately, that will not happen.
Why not? It's your autoload code.
How? After I make the initial call to RandomName_MyFunction() I will get an undefined function error.
Re: Is it OK to use purely static class as a namespace?
Posted: Thu Apr 02, 2009 8:40 pm
by Chris Corbyn
arborint wrote:kaisellgren wrote:It will automatically load the correct library (spl auto loading), but with your RandomName_MyFunction(), unfortunately, that will not happen.
Why not? It's your autoload code.
Yep, you can't autoload functions. Only classes.
Re: Is it OK to use purely static class as a namespace?
Posted: Fri Apr 03, 2009 12:55 am
by Apollo
kaisellgren wrote:It will automatically load the correct library (spl auto loading), but with your RandomName_MyFunction(), unfortunately, that will not happen.
Ah yes, you're right. I stand corrected sir
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?
Posted: Fri Apr 03, 2009 1:02 am
by Benjamin
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
What problems do you experience with irrelevant code being included when debugging? Are you using globals?
Re: Is it OK to use purely static class as a namespace?
Posted: Fri Apr 03, 2009 1:46 am
by Apollo
astions wrote:What problems do you experience with irrelevant code being included when debugging? Are you using globals?
Sometimes, yes (because imho it's useless to enforce an OOP approach for things that are really just static / global).
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.
Re: Is it OK to use purely static class as a namespace?
Posted: Fri Apr 03, 2009 5:46 am
by kaisellgren
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 ?

Re: Is it OK to use purely static class as a namespace?
Posted: Fri Apr 03, 2009 6:16 am
by Apollo
kaisellgren wrote:Anyway, so, do you guys allow me to use classes for namespaces ?

No objectives here
