PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon Sep 16, 2019 12:12 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Sun Feb 13, 2011 1:26 pm 
Offline
DevNet Master
User avatar

Joined: Thu Mar 15, 2007 6:28 pm
Posts: 2765
Location: Redding, California
I'm just wondering, what are static classes/methods actually for? The only application I can think of is a singleton. Otherwise, when is a good time for a static class?


Top
 Profile  
 
PostPosted: Sun Feb 13, 2011 3:01 pm 
Offline
Site Admin
User avatar

Joined: Tue Dec 23, 2003 3:10 am
Posts: 11470
Location: Toronto
Factories, registries, etc.

Basically, anytime you want to maintain a single instance of something (an object, array, etc) static is a likely candidate. It's generally best to avoid static's when possible, since they are harder to test, and remove the possibility of dependency injection, however, they are extremely useful as helper methods.


Top
 Profile  
 
PostPosted: Mon Feb 14, 2011 4:35 am 
Offline
DevNet Resident
User avatar

Joined: Thu May 14, 2009 3:00 pm
Posts: 1011
Location: HellCity, Bulgaria
By definition, the static method, and the static members, belong to the class itself, and not to it's instance. The static methods have to be stateless - they should not depend on any previous calls to the method or to other static methods. You can have a static method if it fits in the above description and you want to provide it's functionality without need to have an instance of the object (if you will use the functionality only with the object's instance, there is no need to make the function static).


Top
 Profile  
 
PostPosted: Mon Feb 14, 2011 11:17 am 
Offline
Briney Mod
User avatar

Joined: Mon Jan 19, 2004 7:11 pm
Posts: 6446
Location: 53.01N x 112.48W
I use static classes whenever I only ever need to do one thing with the class. An email sending class for example, or an error recording class.

_________________
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group