Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static classes in PHP via abstract keyword?

Tags:

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.)

like image 522
Boldewyn Avatar asked Mar 22 '10 17:03

Boldewyn


People also ask

Can we use abstract keyword with static method?

Declaring abstract method static If you declare a method in a class abstract to use it, you must override this method in the subclass. But, overriding is not possible with static methods. Therefore, an abstract method cannot be static.

Can abstract class have static method in PHP?

The Y() that function X() is trying to call is the parent class Y(), which is itself an abstract function. So, using abstract and static on the same method defeats each other purpose. This is the reason why PHP 5.2+ does not allow abstract static class methods.

Are static classes abstract?

static keyword in class definition means that all methods in the class are static as well. But static methods cannot be inherited or overridden, and that is why they can't be abstract.

Can we use this keyword in static method PHP?

$this is not available in static methods. To simplify the reasoning, think of static methods as glorified global functions.


1 Answers

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 :

  • That defines __construct and __clone as private methods
    • so the class cannot be instanciated from outside
  • And, this way, your class could create an instance of itself
    • See the Singleton design pattern, about that, btw


Now, why use a Singleton, and not only static methods ? I suppose that, at least a couple of reasons can be valid :

  • Using a singleton means using an instance of the class ; makes it easier to transform a non-singleton class to a singleton one : only have to make __construct and __clone private, and add some getInstance method.
  • Using a singleton also means you have access to everything you can use with a normal instance : $this, properties, ...
  • Oh, a third one (not sure about that, but might have its importance) : with PHP < 5.3, you have less possibilities with static methods/data :
    • __callStatic has only been introduced in PHP 5.3
    • There is no __getStatic, __setStatic, ...
    • Same for a couple of other Magic methods !
  • Late Static Binding has only been added with PHP 5.3 ; and not having it often makes it harder, when working with static methods/classes ; especially when using inheritance.


This being said, yes, some code like this :

abstract class MyClass {
    protected static $data;
    public static function setA($a) {
        self::$data['a'] = $a;
    }
    public static function getA() {
        return self::$data['a'];
    }
}

MyClass::setA(20);
var_dump(MyClass::getA());

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).

like image 79
Pascal MARTIN Avatar answered Oct 29 '22 22:10

Pascal MARTIN