PHP Doesn't supporting polymorphism concept method overloading, Instead of this, php provides solution by the magic characters.
In the below program __call() is one of the magic method which is automatically called, when any one of the instance methods are called.
<?php
class methodOverLoading
{
public function __call($fname,$arguments)
{
if($fname==='sum')
{
if(count($arguments)===0){
return $this->sum();
}
if(count($arguments)===1){
return $this->sum2($arguments[0],$arguments[1]);
}
}
}
public function sum()
{
echo "OutPut from sum()<br/>";
}
public function sum2($x,$y)
{
echo "Output from sum2()<br/>";
}
}
$omol=new methodOverLoading();
$omol->sum();
$omol->sum2();
?>
Output:
Method OverLoading doesn't supporting example
Result = 20
Method OverRidding supporting example
Parent : Passing value to parent
Child : Passing Value to Child
OutPut from sum()
Output from sum2()
In the below program __call() is one of the magic method which is automatically called, when any one of the instance methods are called.
<?php
class methodOverLoading
{
public function __call($fname,$arguments)
{
if($fname==='sum')
{
if(count($arguments)===0){
return $this->sum();
}
if(count($arguments)===1){
return $this->sum2($arguments[0],$arguments[1]);
}
}
}
public function sum()
{
echo "OutPut from sum()<br/>";
}
public function sum2($x,$y)
{
echo "Output from sum2()<br/>";
}
}
$omol=new methodOverLoading();
$omol->sum();
$omol->sum2();
?>
Output:
Method OverLoading doesn't supporting example
Result = 20
Method OverRidding supporting example
Parent : Passing value to parent
Child : Passing Value to Child
OutPut from sum()
Output from sum2()
(o)
ReplyDelete