Redeem Now
Read More
Read now
Menu Close
    Start a Project Request quote Reading list Switch to dark mode

    Overriding/Static function call with static Keyword in PHP

    In this blog, we will learn how we can call overridden static functions from the base class If that function is overridden by the derived class.

    Lets understand it with an example of problem and solution code –

    What’s The Problem ?

    Suppose you create two static methods in a class BaseClass.
    You have called the first static method getStaticText() from the second static method getStaticResult().

    Now a child class DerivedClass extends the BaseClass and override the static method getStaticText().

    See the below piece of code to understand it properly –

    Now if you call getStaticResult() from DerivedClass

    then the result of var_dump(DerivedClass::getStaticResult()) will be :

    Called from BaseClass“.

    The getStaticText() function called from BaseClass as it is called with self keyword.

    Case :

    You have overrided the getStaticText() method in the DerivedClass.

    Also you have called the method from DerivedClass – var_dump(DerivedClass::getStaticResult());

    Here Let’s suppose you want, if any method is overridden in the child class then it must be called from the child class. If calling class is child class (DerivedClass).

    In the above code I want getStaticText() must be called from DerivedClass.

    • You may say call DerivedClass::getStaticText(); in place of self::getStaticText(); in BaseClass. But how someone knows while creating BaseClass that which class is going to extend BaseClass.

    Then how to achieve this generically?

    Solution 🙂 :

    Here comes the call of static method with static keyword.

    If you use static keyword to call the static functions in the class then –

    • In case there is no overridden function then it will call the function within the class as self keyword does.
    • If the function is overridden then the static keyword will call the overridden function in the derived class.

    In the below code we have used static::getStaticText() in place of self::getStaticText() in the getStaticResult() method of BaseClass.

    Now, the result of var_dump(DerivedClass::getStaticResult()) will be :

    Called from DerivedClass“.

    So the overridden getStaticText() function called this time. This is how static function call with static function works. To know more usees of static visit Static Keyword.

    Hope you have understood the concept and use of static method for overrided static methods and it will help you somewhere in development.

    Happy Coding! 🙂

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*

    Be the first to comment.

    Back to Top
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again