
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
Function Overloading and Overriding in PHP
Function Overloading in PHP
Function overloading is a feature that permits making creating several methods with a similar name that works differently from one another in the type of the input parameters it accepts as arguments.
Example
Let us now see an example to implement function overloading−
<?php class Shape { const PI = 3.142 ; function __call($name,$arg){ if($name == 'area') switch(count($arg)){ case 0 : return 0 ; case 1 : return self::PI * $arg[0] ; case 2 : return $arg[0] * $arg[1]; } } } $circle = new Shape(); echo $circle->area(3); $rect = new Shape(); echo $rect->area(8,6); ?>
Output
This will produce the following output−
9.42648
Function Overriding in PHP
In function overriding, the parent and child classes have the same function name with and number of arguments
Example
Let us now see an example to implement function overriding−
<?php class Base { function display() { echo "
Base class function declared final!"; } function demo() { echo "
Base class function!"; } } class Derived extends Base { function demo() { echo "
Derived class function!"; } } $ob = new Base; $ob->demo(); $ob->display(); $ob2 = new Derived; $ob2->demo(); $ob2->display(); ?>
Output
This will produce the following output−
Base class function! Base class function declared final! Derived class function! Base class function declared final!
- Related Articles
- Difference Between Function Overloading and Overriding in C++
- Difference between Method Overloading and Method Overriding in Java
- What is overriding and overloading under polymorphism in java?
- PHP Overloading
- Function overloading and const keyword in C++
- Function overloading and return type in C++
- What is method overloading in PHP?
- What is function overloading in JavaScript?
- What is the difference between function overriding and method hiding in C#?
- What are the best practices for function overloading in JavaScript?
- str_starts_with and str_ends_with function in PHP 8
- Overriding in C#
- strcmp() function in PHP
- strcoll() function in PHP
- strcspn() function in PHP

Advertisements