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:



filter_none

edit
close

play_arrow

link
brightness_4
code

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();
chevron_right

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.


Looking for a job as a software developer Passing out from college in August, 2021

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :