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 :
__construct
and__clone
as private methodsNow, why use a Singleton, and not only static methods ? I suppose that, at least a couple of reasons can be valid :
__construct
and__clone
private, and add somegetInstance
method.$this
, properties, ...__callStatic
has 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).
abstract
really is meant to indicate a "blueprint", as you call it, for class inheritance.Registries generally follow a singleton pattern, which means they it would instantiate itself in a private variable. Defining it as
abstract
would prevent this from working.There are patterns in OO that are common and well-recognized. Using
abstract
in an unconventional way may cause confusion (sorry, my some examples are in Java instead of PHP):Commonly implemented by:
abstract
modifier on class to prevent direct instantiation, but allow deriving from the classCommonly implemented by:
final
modifier on class, andCommonly implemented by:
final
modifier on class, andgetInstance()
) that returns the only instance or one of the limited number of instancesSetting a class to abstract that only defines static properties/methods won't have a real effect. You can extend the class, instantiate it, and call a method on it and it would change the static class properties. Obviously very confusing.
Abstract is also misleading. Abstract is ment to define an class that implements some functionality, but needs more behaviour (added via inheritance) to function properly. On top of that it's usually a feature that shouldn't be used with static at all. You are practically inviting programmers to use it wrong.
Short answer: A private constructor would be more expressive and fail safe.