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?
Static Class and singleton (single class instances)
Moderator: General Moderators
Re: Static Class and singleton (single class instances)
You should use static method to make singleton pattern.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?
Could you give examples as to what you mean by static classes only. What two ways you see of inforcing singleton pattern.
As stated in the PHP manual:
By static class, I just mean a class entirely of static methods and members. Example: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.
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)
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 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?
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?
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?
Without explaining all this I found this link which should answer your question...
http://www.artima.com/designtechniques/static.html
http://www.artima.com/designtechniques/static.html
Last edited by alvinphp on Wed Dec 28, 2005 2:13 pm, edited 1 time in total.