Function Overriding | Object Oriented Concepts in PHP

We discussed Inheritance in PHP in an earlier post. We learned that a Child Class can inherit the properties of a Parent Class. Function Overriding allows the Child Class to have the same method which already exists in the Parent Class. By overriding the method we can modify the definition of a function inherited from the Parent Class.

What is Function Overriding?

Function Overriding is a very important concept in Object Oriented Programming. It allows the Child Class to alter the implementation of a method in Parent Class by overriding it. The method in the Child Class should have the same name and signature as the Parent Class. However, since Private methods are not accessible to the Child Class, we can only override Public and Protected methods.

Overriding in PHP

For instance, let’s consider a basic example for overriding methods in PHP.

<?php
	
	class BaseClass {

		public function printSomething() {
			echo 'Printing From BaseClass';
		}
	}

	class ChildClass extends BaseClass {
		public function printSomething() {
			echo 'Printing From ChildClass';
		}
	}

	$childObject = new ChildClass();
	$childObject->printSomething();
        /*
        OUTPUT:
        Printing From ChildClass
        */
?>

In the above code snippet, we have a Base Class and A Child Class that is inheriting from that Base Class. We override the printSomething() method by declaring it in the Child Class also. The child class version of the method will execute on invoking the method from an object of Child Class.

Invoking Sequence

The Class Object determines which method is invoked. The child class version is invoked if the child object was used to invoke the method. However, if the created object belongs to the Parent Class, then the parent method will be invoked.

Invoking The Parent Method

How about a situation when you deliberately want to call the parent method? The ‘parent’ keyword can be used to invoke the parent method statically.

<?php
	
	class BaseClass {

		public function printSomething() {
			echo 'Printing From BaseClass';
		}
	}

	class ChildClass extends BaseClass {
		public function printSomething() {
			parent::printSomething();
			echo 'Printing From ChildClass';
		}
	}

	$childObject = new ChildClass();
	$childObject->printSomething();
	/*
	OUTPUT:
	Printing From BaseClass
	Printing From ChildClass
	*/
?>

As you can observe in the above output, it first invokes the parent method. Afterward, it executes the code in the child method.

Note: Never ever invoke the parent method using $this object. Since $this is the reference to the calling object, it will call the Child Method in an infinite loop.

You may also read about the Static Keyword on Concatly.

Overriding The Constructor

Similarly, you can override the Base Class Constructor also.

<?php
	
	class BaseClass {

		public function __construct() {
			echo 'Base Constructor Called';
		}

	}

	class ChildClass extends BaseClass {
		public function __construct() {
			parent::__construct();
			echo 'Child Constructor Called';
		}
	}

	$childObject = new ChildClass();
	/*
        Base Constructor Called
        Child Constructor Called
        */
?>

By statically calling the parent constructor, we can call the base constructor first and then execute the code in the child constructor.

Special Cases With Constructors

If you don’t declare a constructor in the child class, then the base class constructor is automatically called while invoking the child object. The parent constructor is called automatically without explicitly doing so. For instance, consider the example below:

<?php
	
	class BaseClass {

		public function __construct() {
			echo 'Base Constructor Called';
		}

	}

	class ChildClass extends BaseClass {
		
	}

	$childObject = new ChildClass();
	/*
	Base Constructor Called
	*/
	
?>

This is the specialty of Constructors. While creating an object of the Child Class, always make sure to invoke the Base Class Constructor. If you dont invoke the Base Class Constructor then the Child Class will not be able to inherit Base Class’s properties. If you declare the Constructor in the Child Class then make sure to explicitly invoke the Parent Constructor.

Conclusion

We discussed Function Overriding in PHP in this article. Also, we discussed overriding Constructors. You can learn more about Inheritance and Function Overriding on the Official Documentation of PHP.

Spread the Knowledge

Leave a Reply

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