Open In App

Why does PHP 5.2+ disallow abstract static class methods ?

Before we answer this question, you must have a clear definition of what exactly is an abstract class, an abstract method and a static method.

Abstract Class: In the Object-Oriented programming paradigm, abstraction refers to the process of hiding the internal implementation details of any program and only showing the functionality of the program to the user. Abstract classes are a way of achieving this. An abstract class is any class that cannot be instantiated (i.e. objects cannot be created) and has to be extended (inherited) for objects to be created. The ‘abstract’ keyword is used to create abstract classes.



Abstract Method: An abstract method is a method that can only be declared and not defined. It is defined in the class that inherits from this class.

Static Method: A static method of any class is a method that is created only once. This means that even if you create hundreds of objects of a class with static methods, there will be the only copy of each static method.



Consider the following example:




abstract class Abstract_Parent {
      
    static function X() {
        self::Y();
    }
      
    abstract static function Y();
}
  
class Child extends Abstract_Parent {
      
    static function Y() {
        echo "GeeksforGeeks";
    }
}
  
Child::X();

Now if you run this PHP code, you will see the error, “PHP Fatal error: Cannot call abstract method Abstract_Parent::X().”

When we call method X() in the child class, the static function X() in the parent gets called. In the method X(), we are again calling function Y(), which is an abstract static function. 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.

Article Tags :