According to the PHP manual, a class like this:
abstract class Example {}
cannot be instantiated. If I need a class without instance, e.g. for a registry pattern:
class Registry {}
// and later:
echo Registry::$someValue;
would it be considered good style to simply declare the class as abstract? If not, what are the advantages of hiding the constructor as protected method compared to an abstract class?
Rationale for asking: As far as I see it, it could a bit of feature abuse, since the manual refers to abstract classes more as like blueprints for later classes with instantiation possibility.
Update: First of all, thanks for all the answers! But many answers sound quite alike: ‘You cannot instantiate an abstract class, but for a registry, why not using a singleton pattern?’
Unfortunately, that was more or less exactly a repeat of my question. What is the advantage of using a singleton pattern (a.k.a. hiding __construct()) compared to just declaring it abstract and not having to worry about that? (Like, e.g., it is a strong connotation between developers, that abstract classes are not actually used or so.)
If your class is not meant to define some super-type, it should not be declared as
abstract, I’d say.In your case, I would rather go with a class :
__constructand__cloneas private methodsNow, why use a Singleton, and not only static methods ? I suppose that, at least a couple of reasons can be valid :
__constructand__cloneprivate, and add somegetInstancemethod.$this, properties, …__callStatichas only been introduced in PHP 5.3__getStatic,__setStatic, …This being said, yes, some code like this :
Will work… But it doesn’t feel quite natural… and this is a very simple example (see what I said earlier with Late Static Binding, and magic methods).