Professional Documents
Culture Documents
Overloading Overriding PHP
Overloading Overriding PHP
several methods with the same name which differ from each other in the type of the
input parameters. It is simply defined as the ability of one function to perform
different tasks. For example, doTask() and doTask(object O) are overloaded methods.
This is erroneous since php will say you have declared this method twice. Other
languages says its fine and calls it static polymorphic i.e method overloading.
<?php
/*
* Don't try this at home, will be fatal !!
class Shape {
function area(){
return 0 ;
}
function area($radius){
return PI * $radius * $radius ;
}
function area($length,$breadth){
return $length * $breadth ;
}
}
Normally method overriding required when your parent class have some method, but in
your child class you want the same method with different behavior. By overriding of
method you can complete change its behavior from parent class. To implment method
overiding in oop we commonly create same method in child class.
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("Hello world");