Quantcast
Channel: Webkul Blog
Viewing all articles
Browse latest Browse all 5537

Overriding/Static function call with static Keyword in PHP

$
0
0

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 –

<?php
	Class BaseClass 
	{
	    // Static functions
	    public static function getStaticText()
	    {
	        return 'Called from BaseClass';
	    }

	    public static function getStaticResult()
	    {
	        // we can alternatively use BaseClass::getStaticText();
	        return self::getStaticText();
	    }
	}

	Class DerivedClass extends BaseClass
	{
	    public static function getStaticText()
	    {	
	        return 'Called from DerivedClass';
	    }
	}

	var_dump(DerivedClass::getStaticResult());

?>

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.

<?php

	Class BaseClass 
	{
	    // Static functions
	    public static function getStaticText()
	    {
	        return 'Called from BaseClass';
	    }

	    public static function getStaticResult()
	    {
	        // we can alternatively use BaseClass::getStaticText();
	        return static::getStaticText();
	    }
	}

	Class DerivedClass extends BaseClass
	{
	    public static function getStaticText()
	    {	
	        return 'Called from DerivedClass';
	    }
	}

	var_dump(DerivedClass::getStaticResult());

?>

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! 🙂


Viewing all articles
Browse latest Browse all 5537

Trending Articles