Overloading :-
Function overloading means you want more work or login from a function or
you can use different login in a function.
You can not use same function in a class so for use this login you can use __call
magic function where you can pass method name and parameter and can add login
according to their parameter.
Take a look on code:-
class test
{
public function __construct()
{
//Your logic for constructor
}
public function __call($method_name , $parameter)
{
if($method_name == "overlodedFunction")
//Function overloading logic for function name overlodedFunction
{
$count = count($parameter);
switch($count)
{
case "1":
//Business log in case of overlodedFunction function has 1 argument
echo "You are passing 1 argument";
break;
case "2": //Incase of 2 parameter
echo "You are passing 2 parameter";
break;
case "3": //Incase of 2 parameter
echo "You are passing 3 parameter";
break;
default:
throw new exception("Bad argument");
}
}
else
{
throw new exception("Function $method_name does not exists ");
}
}
}
$a = new test();
$a->overlodedFunction("sagar");
$a->overlodedFunction("techflirt" , "sagar","5454");
Overriding :- Function overridng means you can override a function
which is mention in parent class by same name of function;
Example:
class Parent
{
public function s1()
{
echo 1;
}
public function s2()
{
echo 2;
}
}
class Child
{
function s2($a) //overriding function s2
{
echo "$a";
}
}
$a = new Child();
$a->s2("sagar");//it will print sagar
Comments
Post a Comment