OOPs Tutorials
- What is OOPs
- Access Modifier
- Constructor
- Destructor
- Polymorphism
- Inheritence
- Method Overriding
- Abstract class
- Interface
- Traits
- This keyword
- static keyword
- Final keyword
- Class Constant
- Exception Handling
- Binding in php
- tostring in php
- Object Iteration
- Reflection
- Magic methods
- Namespaces
- CRUD Function
- OOPs CRUD
- CRUD Example
Important Links
What is Polymorphism?
Polymorphism is a concept in which a class containg more than one method with the same name but behaviour of each method is different is called polymorphism.
There are two types of polymorphism.
Method overloading
you can aslo overload methods in php.
Method overloading is a mechanism in which a class contain more than one method with the same name but parameter is different is called method overloading.
we can not implement overloading by create 2 function in with same name in class. So to implement overloading in php we will take help of magic method __call. Magic method __call invoked when method called by class object is not available in class. So here we will not create method exactly and will take help of __call method. Now call method will provide us 2 argument, 1st name of the method called and parameter of the function. Now with the help of either switch , case or if else we will implement overloading in php. Following is very simple example of overloading in php.
File name : index.php
<?php
function set_name($data)
{
$this->name=$data;
}
function set_name($data, $msg)
{
$this->name=$data;
$this->message = $msg;
}
$obj->set_name("mahtab");
$obj->set_name("mahtab", "alam");
?>
In php, you implement method overloading with the __call( ) method.
In a class, this is the method that gets called when you call a method that doesn’t exist.
You have a class that has a __call ( ) method, but no set_name( ) method. When you call the set_name( ) method on objects of that class, what actually gets called is the __call( ) method. The __call( ) method is passed the name of the missing method, as well as an array holding the arguments list that was passed to that missing method.
function __call($method, $arguments)
{
//…………………..
}
For example
If you wanted to overload the set_name( ) method to be callable as both set_name($name) or set_name($name, $message), you could leave that method unwritten, and handle it with a __call( ) method.
In the __call( ) method, you could first check if the method called (and not found) was indeed set_name( ).
function set_name($method, $arguments)
{
if($method == “set_name”)
{
//………………….
}
}
The set_name( ) method can be called with one or two arguments($name or $name and $message), so you can check first if a single arguments was passed.
function __call($method, $arguments)
{
if($method == “set_name”)
{
if(count($arguments) == 1)
{
}
}
}
If a single arguments was passed, that’s the person’s name, and you can store it like this
function __call($method, $arguments)
{
if($method == “set_name”)
{
if(count($arguments) == 1)
{
$this->name = $arguments[0];
}
}
}
On the other hand, if two arguments were passed to the function, those arguments are $name and $message.
function __call($method, $arguments)
{
if($method == “set_name”)
{
if(count($arguments) == 1)
{
$this->name = $arguments[0];
}
if(count($arguments) == 2)
{
$this->name = $arguments[0];
$this->message = $arguments[1];
}
}
}
File name : index.php
<?php
Class Friend{
var $name; var $message;
function speak( ){
echo $this->name, “says \””, $this->message, “\”<br>”;}
function set_message($msg){
$this->message = $msg; }
function __call($method, $arguments)
{ if($method == “set_name”){
if(count($arguments) == 1)
{
$this->name = $arguments[0];
}
if(count($arguments) == 2)
{
$this->name = $arguments[0];
$this->message = $arguments[1];
}
}
}
}
echo “creating your new friend … <br>”;
$obj = new Friend;
$obj->set_name(“mahtab”);
$obj->set_message(“Hello India”);
$obj->speak( );
$obj->set_name(“danish”, “khan”);
$obj->speak( );
?>
Method overriding
Method overriding means that you can redefine a base class method in a derived class. 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.
In OOP PHP if we were to create a method in the child class having the same name, same number of parameters and the same access specifier as in it’s parent then we can say that we are doing method overriding. for example:
File name : index.php
class A
{
public function test($param)
{ echo "\n Parent - the parameter value is $param";
}
}
class B extends A
{
public function test($param)
{ echo "\n Child - the parameter value is $param";
}
}
$objA = new A;
$objB = new B;
$objA->test('class A');
$objB->test('class B');
The definition of polymorphism says that “if the decision to invoke a method is made by inspecting the object at run time then it is a case of polymorphism”. We can apply this rule in our example. Whether to call method test() of class A or class B is decided by inspecting the concerned object which is making the call to the method. Thus the method call will be based on the object.
File name : index.php
class AA
{
public function output($args)
{ echo "\n Parent - the parameter value is $args";
}
}
class BB extends AA
{
public function output($args)
{
echo "\n Child - the parameter value is $args";
}
}
$obj1 = new AA;
$obj2 = new BB;
$obj1->output('class AA');
$obj2->output('class BB');
File name : index.php
<?php
class Person
{
var $name;
function set_name($data)
{
$this->name = $data;
}
function get_name()
{
return $this->name;
}
}
?>
<?php
class Person
{
var $name;
function set_name($data)
{
$this->name = $data;
}
function get_name()
{
return $this->name;
}
}
?>
File name : index.php
Class Friend extends Person
{
var $name;
function speak( )
{
echo $this->name, “ is speaking <br>”;
}
function set_name($data)
{
$this->name = strtoupper($data);
}
}
echo “creating your new friend …<br>”;
$obj = new Friend;
$obj->set_name(“mahtab”);
$obj->speak( );
?>
Output :-
Crating your new friend…
Mahtab is speaking.
File name : index.php