What is polymorphism in php?
Polymorphism in PHP is a concept by which we can perform a single action in different ways. Polymorphism is derived in two from Greek word “poly” and “morphism”.
"Poly" means many and "morphism" means (forms) property which help us to assign more than one property.
# Types of polymorphism :
There are two types of Polymorphism:
Overriding(Run-time polymorphism).
Overloading(Compile-time polymorphism).
1.) Method Overriding : Method overriding is one of the ways in which Php supports Runtime Polymorphism. If same functions defined in parents and child class with same signature know as function overriding.
It is also known as dynamic binding or late binding.
Let’t see an example of Function overriding :
➤ Example : function overriding;
<?php
class ParentClass {
public function myMessage(){
echo "Parent-Class method called";
}
}
class ChildClass extends ParentClass{
public function myMessage(){
echo "Child-Class method called";
}
}
$obj = new ChildClass(); // Creating object for ChildClass
$obj -> myMessage(); // Method callng
?>
Output :
Child-Class method called
2.) Overloading : If a class has multiple functions having same name but different in parameters, It is known as function overloading.
It contains the same function name and that function preforms different tasks according to the number of arguments.
It is also known as static or early binding.
Note : Like other OOP programming languages function overloading, PHP can not be done by native approach.
In PHP function overloading is done with the help of magic function __call() and this function takes parameter( name and arguments).
Let's see, by the simple example :
➤ Example : overloading;
<?php
class Overloading{
function sum($a, $b){
echo $a+$b;
}
function sum($a, $b, $c){
echo $a+$b+$b;
}
}
$obj = new Overloading(); // Creating object
$obj -> sum(10, 20);
$obj -> sum(11, 22, 33);
?>
Output :
Fatal error: Cannot redeclare PropertyOverload::sum() in Overloading.php on line 6
Important "Property" and "Rules" of overloading in PHP:
overloading methods must be defined as public
After creating the object for a class, we can access a set of entities that are methods or properties not defined within the scope of the class.
Such entities are said to be overloaded properties or methods, and the process is called as overloading.
For working with these overloaded properties or methods, PHP magic methods are used.
Most of the magic methods will be triggered in object context except __callStatic() method which is used in a static context.
There are two types of overloading in PHP:
Property overriding.
Method overloading.
2.1 ) Property Overriding : PHP property overloading is used to create dynamic properties in the object context.
A property associated with a class instance, and if it is not declared within the scope of the class, it is considered as overloaded property..
Note : For creating these properties no separate line of code is needed.
There are following operations performed with overloaded properties in PHP:
Setting and getting overloaded properties.
Evaluating overloaded properties setting.
Undo such properties setting.
-
Before performing the operations, we should define appropriate magic methods. which are,
__set() - __set() method is run when writing data to inaccessible (private or protected) or non-existing properties.
__get() - __get() methog is utilized for reading data from inaccessible (private or protected) or non-existing properties.
__isset() - __isset() magic method is invoked when we check overloaded properties with isset() function
__unset() - Similarly, this function will be invoked on using PHP unset() for overloaded properties.
Let's try, to create simple example :
➤ Example : property overriding;
<?php
class PropertyOverloading {
// Location of overloading data
private $data = array();
// Overloading not used on declared properties.
public $declared = 1;
// Overloading only used on when accessed outside the class.
private $hidden = 2;
// Function definition
public function __set($name, $value){
echo "Setting '$name' to '$value'\n";
$this->data[$name] = $value;
}
// Function definition
public function __get($name){
echo "Getting '$name': ";
if(array_key_exists($name, $this->data)){
return $this->data[$name];
}
$trace = debug_backtrace();
return null;
}
// Function definition
public function __isset($name){
echo "Is '$name' set? \n";
return isset($this->data[$name]);
}
// Definition of __unset function
public function __unset($name){
echo "Unsetting '$name'\n";
unset($this->data[$name]);
}
// getHidden functinon definition
public function getHidden(){
return $this->hidden;
}
}
$obj = new PropertyOverloading(); // Create an object
// Set value 1 to the object variable
$obj->a = 1;
echo $obj->a . "\n";
// Use isset function to check
var_dump(isset($obj->a)); // 'a' is set or not
unset($obj->a); // Unset 'a'
var_dump(isset($obj->a));
echo $obj->declared . "\n\n";
echo "Private property are visible inside the class";
echo $obj->getHidden() . "\n\n";
echo "Private property are not visible outside of class\n";
echo $obj->hidden . "\n";
?>
Output :
Setting 'a' to '1'
Getting 'a: 1
Is 'a' set?
bool(true)
Unsetting 'a'
Is 'a' set?
bool(false)
1
Private property are visible inside the class 2
Private property are not visible outside of class
Getting 'hidden:
2.2 ) Method Overloading : It is a type of overloading for creating dynamic methods that are not declared within the class scope. PHP method overloading also triggers magic-methods dedicated to the appropriate purpose, Unlike property overloading.
PHP method overloading allows function call on both static and object context, The related magic functions are following,
__call() - It is triggered when invoking inaccessible methods in the object context.
__callStatic() - It is triggered when invoking inaccessible methods in static context.
Note : value of $name in '__call' and '__callStatic' is case sensitive.
Let's try, to create simple example :
➤ Example : method overriding;
<?php
class MethodOverloading {
public function __call($name, $arguments){
echo "Calling object method '$name'"
. implode(', ', $arguments) . "\n";
}
public static function __callStatic($name, $arguments){
echo "Calling static method '$name'"
. implode(', ', $arguments) . "\n";
}
}
$obj = new MethodOverloading(); // Create an object
$obj -> runTest('in object context');
MethodOverloading::runTest('in static context');
?>
Output :
Calling object method 'runTest' in object context
Calling static method 'runTest' in static context
Comments