Using Static Class Functions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Using Static Class Functions

Post by harrisonad »

I am aware of the common (ei. standard) use of static class functions using the operator "::". What is the reason for using this technique? I mean, why not use a function in ordinary way, such as "HTML::CreateForm();" to just "CreateForm();" function call?

Thanks in advance for your reply...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

some are for organizational things. I use them a lot because of potential function name conflicts, or when I don't want people to instance the class they are housed in. This is similar to the concept of namespaces in C.

The SHA256 class I wrote for instance, isn't meant to be instantiated. Partly because of the generic interface it uses, and partly because getting a new hash would require creating a new instance of the class, pretty much.

Unless you name your functions VERY unique names, I'd expect them to collide with some other library at some point, especially if they are quite generic names, such as CreateForm()
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

RE

Post by harrisonad »

Thanks pal,Now I know...
Yet, as a follow-up question, Which executes faster: a regular function class or that of a class?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

do you mean a "standard" function versus a class method?

I haven't done any tests on the subject, but I'd guess the class would be very very very slightly slower.. but that depends on how PHP's core handles those kinds of things. In C, there's rarely much of a difference.. but that depends on what you are doing in it, and how you are doing it.
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

RE

Post by harrisonad »

What i mean is:
Which executes faster;

echo Tag('TR');

or...


echo HTML::Tag('TR');
Last edited by harrisonad on Sun Apr 03, 2005 10:19 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

why not test it for yourself? :)
Post Reply