Is it OK to use purely static class as a namespace?

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
User avatar
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?

Post 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"?
User avatar
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?

Post by Christopher »

Can I just say NO and that's the end of it? ;)

I would recommend using PEAR naming until then.
(#10850)
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: Is it OK to use purely static class as a namespace?

Post 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.
User avatar
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?

Post 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:

Code: Select all

RandomName::MyFunction();
It will automatically load the correct library (spl auto loading), but with your RandomName_MyFunction(), unfortunately, that will not happen.
User avatar
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?

Post 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.
(#10850)
User avatar
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?

Post 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.
User avatar
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?

Post 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.
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: Is it OK to use purely static class as a namespace?

Post 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 ;)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Is it OK to use purely static class as a namespace?

Post 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?
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: Is it OK to use purely static class as a namespace?

Post 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.
User avatar
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?

Post 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 ? :)
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: Is it OK to use purely static class as a namespace?

Post by Apollo »

kaisellgren wrote:Anyway, so, do you guys allow me to use classes for namespaces ? :)
No objectives here :)
Post Reply