Page 1 of 1
Static Class and singleton (single class instances)
Posted: Tue Dec 27, 2005 1:16 am
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?
Re: Static Class and singleton (single class instances)
Posted: Tue Dec 27, 2005 4:09 am
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.
Posted: Tue Dec 27, 2005 3:04 pm
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');
Re: Static Class and singleton (single class instances)
Posted: Tue Dec 27, 2005 3:09 pm
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.
Posted: Tue Dec 27, 2005 3:57 pm
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?
Posted: Tue Dec 27, 2005 5:52 pm
by Gambler
You can pass singleton object as an argument, while class with static methods can be used only directly.
Posted: Tue Dec 27, 2005 8:17 pm
by alvinphp
Without explaining all this I found this link which should answer your question...
http://www.artima.com/designtechniques/static.html
Posted: Tue Dec 27, 2005 9:26 pm
by Cogs
Thanks!
This is what I was looking for!