Skip to content
CS Chetan Patil
About Projects Blog Gallery Contact
Resume
  1. Home
  2. Blog
  3. OOP in PHP

OOP in PHP 8 July 2016 · 2 min read

Overriding in PHP

Method Overriding in OOP ? Basic meaning of overriding in oop is same as real word meaning. In real word meaning of overriding phenomena of replacing the same parental behaviour in child. This is same in case of method overriding in oop…

Chetan S. Patil Software Engineer at Genpact

Method Overriding in OOP ? #

Basic meaning of overriding in oop is same as real word meaning. In real word meaning of overriding  phenomena of replacing the same parental behaviour in child. This is same in case of method overriding in oop. In oop meaning of overriding is to replace parent class method in child class. Or in simple technical word method overriding mean changing behaviour of the method. In oop overriding is process by which you can re-declare your parent class method in child class. So basic meaning of overriding in oop is to change behaviour of your parent class method.

Normally method overriding required when your parent class have some method, but in your child class you want the same method with different behaviour. By overriding of method you can complete change its behaviour from parent class. To implement method overriding in oop we commonly create same method in child class.

Overriding in php is very easy. As we know that overriding is process of modifying the inherited method. So in case of inheritance you only need to create method with same name in your child class which you want to override. Following is example of overriding of method in php.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class testParent
{
public function f1()
{
echo 1;
}
public function f2()
{
echo 2;
}
}
class testChild
{
function f2($a) //overriding function f2
{
echo "$a";
}
}
$a = new testChild();
$a->f2("ankur");//it will print ankur

In above example you are overriding function f2. While overriding you are free to change business logic, visibility and number of parameter.

For more detail about overloading and overriding in php you can read:
http://php.net/manual/en/language.oop5.overloading.php

Share this
Chetan S. Patil

Written by

Chetan S. Patil

Software Engineer in Pune — PHP, Symfony and SQL Server behind financial operations and payment systems.

About LinkedIn
PreviousOverloading in PHP NextInheritance in PHP

Related reading

All articles

OOP in PHP · 3 min read

Dependency injection

Dependency injection is a technique where one object supplies the dependencies of another object. An injection is the passing of a dependency…

15 Jun 2017

OOP in PHP · 2 min read

Constructor

What does Constructor mean? A constructor is a special method of a class or structure in OOP that initializes an object of…

20 Jan 2017

OOP in PHP · 7 min read

Dependency injection in PHP

Dependency Injection is a software design pattern that allows avoiding hard-coding dependencies and makes possible to change the dependencies both at runtime…

12 Jul 2016

CS Chetan Patil

Software Engineer in Pune — PHP, Symfony and SQL Server behind financial operations and payment systems.

i@chetanpatil.co.in

Site

About Projects Blog Gallery Resume Contact RSS

Elsewhere

Pune, Maharashtra · IST (UTC+5:30)

© 2026 Chetan S. Patil

Built with PHP & MySQL

Chetan Patil
About Projects Blog Gallery Contact Resume
i@chetanpatil.co.in