Static Class and singleton (single class instances)

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
Cogs
Forum Newbie
Posts: 17
Joined: Thu Mar 27, 2003 4:57 pm

Static Class and singleton (single class instances)

Post by Cogs »

Hi,

In cases where you should not have multiple instances. Why would you addopt the singleton pattern, where you only use a single instance of a class, as opposed to just using static classes? Is the only benifit of the later the support for constructors?

When should either be used?
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Re: Static Class and singleton (single class instances)

Post by jmut »

Cogs wrote:Hi,

In cases where you should not have multiple instances. Why would you addopt the singleton pattern, where you only use a single instance of a class, as opposed to just using static classes? Is the only benifit of the later the support for constructors?

When should either be used?
You should use static method to make singleton pattern.
Could you give examples as to what you mean by static classes only. What two ways you see of inforcing singleton pattern.
Cogs
Forum Newbie
Posts: 17
Joined: Thu Mar 27, 2003 4:57 pm

Post by Cogs »

As stated in the PHP manual:
The Singleton pattern applies to situations in which there needs to be a single instance of a class. The most common example of this is a database connection. Implementing this pattern allows a programmer to make this single instance easily accessible by many other objects.
By static class, I just mean a class entirely of static methods and members. Example:

Code: Select all

class Registry
{
   public static $vars = array();

   public static function set() {
       // ...
   }

   public static function get() {
       // ...
   }
}


Register::set('x', 'a')
$x = Register::get('x');
alvinphp
Forum Contributor
Posts: 380
Joined: Wed Sep 21, 2005 11:47 am

Re: Static Class and singleton (single class instances)

Post by alvinphp »

Cogs wrote:Hi,

In cases where you should not have multiple instances. Why would you addopt the singleton pattern, where you only use a single instance of a class, as opposed to just using static classes? Is the only benifit of the later the support for constructors?

When should either be used?
I admit, I have not really followed 'patterns' much even though a I lot of what I do seems to fit some 'pattern'. As for using a static class vs an object (an instance of a class) I always use the object. And it is not just for constructors as it can be used to change an attribute during any method which will effect methods called after. Creating and using an object is pretty fundamental to OOP.
Cogs
Forum Newbie
Posts: 17
Joined: Thu Mar 27, 2003 4:57 pm

Post by Cogs »

By "change an attribute" do you mean using object variables? Because with PHP5 you can use static class variables, which is a part of the reason why I bring this question up. I am reworking an application to PHP 5 (from PHP 4). Where before we had a singleton object factory to create a single instance of a class, we could probably use static class members and methods instead and remove the need for the singleton "factory" as classes exist on the global scope and you can access methods without needing to retrieve/create an instance of the class.

Since we can use static members, and static methods, is there any reason why we should not just stick purely with static classes when multiple instances are not required?
Gambler
Forum Contributor
Posts: 246
Joined: Thu Dec 08, 2005 7:10 pm

Post by Gambler »

You can pass singleton object as an argument, while class with static methods can be used only directly.
alvinphp
Forum Contributor
Posts: 380
Joined: Wed Sep 21, 2005 11:47 am

Post by alvinphp »

Without explaining all this I found this link which should answer your question...

http://www.artima.com/designtechniques/static.html
Last edited by alvinphp on Wed Dec 28, 2005 2:13 pm, edited 1 time in total.
Cogs
Forum Newbie
Posts: 17
Joined: Thu Mar 27, 2003 4:57 pm

Post by Cogs »

Thanks! :D

This is what I was looking for!
Post Reply