Home » Polymorphism in PHP

Polymorphism in PHP

by Online Tutorials Library

Polymorphism in PHP

Polymorphism is gotten from the Greek Word Poly (and that implies many) and morphism (it implies important structures).

Polymorphism is one of the four pillars that depicts a model in object-oriented programming, also known as OOPs. It is an OOP design that empowers classes with various functionalities to execute or share a common interface. The helpfulness of polymorphism is code written in various classes has no impact on which class it has a place since they are utilized similarly. There were techniques in different classes that do the same things should have the same name. To guarantee that the classes do carry out the polymorphism rule, we can pick between one of the two choices of either abstract classes or interfaces.

Object-Oriented Programming:

  • OOPs, also called Object Oriented Programming, are organized programming techniques used to put together code blocks around information and items rather than logic and functions.
  • Objects created while working with object-oriented programming language are information fields with particular trademarks and characteristics.
  • Object oriented programming mostly centres around the information objects made by the engineer that they need to control to play out a specific assignment instead of zeroing in on the rationale expected to control the object.
  • This kind of writing a computer program is exceptionally viable for programs with extremely high intricacy or information that should be refreshed routinely or kept up routinely.
  • A few different benefits of OOPs incorporate code reusability, versatility, and effectiveness.

The fundamental guideline of OOP comprises of:

  1. Data Encapsulation
  2. Data Abstraction
  3. Inheritance
  4. Polymorphism

Polymorphism in PHP

Usually, polymorphism is of two types

  • Compile time polymorphism, which is also known as function overloading
  • Run time polymorphism, which is also called function overriding

NOTE: – PHP does not support compile-time polymorphism, which means we cannot use polymorphism properties like function overloading and operators overloading, which can be very useful in many scenarios. Further in this article, the entire polymorphism properties will be taught using the run time property of polymorphism, or function overriding.

Runtime polymorphism

The Runtime polymorphism implies that a choice is made at runtime (not at the time of compilation), or we can have various subtypes carried out for a super class. Function overloading is an illustration of runtime polymorphism.

Function overriding – Whenever, we make a function in a determined class with a similar mark, i.e., all in all, a function has a similar name, a similar number of arguments, and a similar kind of parameters as a function in its parent class it is called function overloading.

Example: The use of run time polymorphism

Output:

Polymorphism in PHP

We have declared one base class or parent class new_shape ( ) in the above program. We have used inheritance property to inherit the properties from the parent class to three child classes circle, Triangle, and Square. Each class has been provided a function draw to show how to use run time polymorphism, and as you can see, the parent class only holds the declaration of the function draw, not its properties. Now to call the classes, we have created an array of length 2 called val.

The array index is used to call each object of each class. And to run the array, we have used a loop to execute the array according to the length of the declared array. The array index will use $i to count till the length of the array, and each time the array repeats, the value of I will be increased. A new class at that particular array position will be called, and the program will automatically execute itself.

Types of PHP polymorphism

PHP can use the method of polymorphism in 2 distinguishing ways. Polymorphism in PHP uses interfaces that are pre-provided in PHP and abstract classes to implement the methods of polymorphism.

Abstract classes and interfaces are special characteristics of single inherited programming languages such as PHP, where multiple inheritances are impossible. it helps the developer reduce the limitations of PHP’s single inheritance property and helps reuse code freely in different classes that present different levels in a hierarchy. These are almost similar to a class, but unlike classes, they consistently group functionality. It is not possible to initiate a interface on its own that’s why it is necessary to use it with class. We can say that these are type of class that helps the developer with multiple inheritances and polymorphism by using multiple functions at the same time

Polymorphism using Interface

Interface – Interface is almost like a class. Aside from that, it can’t contain code. The Interface can declare arguments and the method name, but only it can declare them. It cannot add the contents of those methods. Any classes that will be going to execute Interface should execute all techniques described by the Interface.

Syntax:

Example of Polymorphism using Interface

Output:

Polymorphism in PHP

In the above program, we have declared one Interface or parent class area ( ). We have used interface property to inherit the properties from the parent class to two child classes, Circle and rectangle. Each class has been provided a function Calcarea ( ) to show how to use run time polymorphism using the interface method. As you can see, the parent class only holds the declaration of the function, not its properties.

As the polymorphism rule says, every one of the strategies that calculate the process would have the same name for this present circumstance. Presently, whenever we would have to figure the Task for the various Classes, we would call a function with the name of calcTask() without giving a ton of thought to the subtleties of how to compute the Task for the different functions. The central thing that we would have to know is the function’s name that computes the Task.

Polymorphism using abstract classes

Abstract classes and techniques are the points at which the parent class has a named strategy yet needs its kid class to finish the undertakings. It is a class that contains no less than one abstract method and is a technique that is only declared, however, not carried out in the code.

The abstract key usually characterizes an abstract class:

While inheriting from an abstract class, the youngster class should be characterized with a similar name and the equivalent of a less limited access modifier / parameter. In this way, if the abstract class is characterized as protected, the youngster class should be characterized as either protected or public. As we all know, it can never inherit private functions by any means possible. Likewise, the sort and number of variables or declared should be similar. Be that as it may, the child classes might have more declared variables or functions.

But while inheriting from an abstract class, it should follow these principles.

  • The child or the inheriting class should be characterized with a similar name, and it re declares the abstract method derived from the parent class.
  • The child class should be characterized with something similar or a less access modifier.
  • The number of variables declared should be something similar. Be that as it may, the child classes might have more declared variables or functions.

Syntax:

Example:

Output:

Polymorphism in PHP

In the above program, we have declared an abstract class, Language, and this abstract class contains an abstract method or abstract function called greet ( ). We have used the property to inherit the properties from the parent class to three English, Spanish and French classes, and each child class will return a different output. Later we have declared a function greeting ( ) that will accept input as an array and will automatically call the greet function.. further to retrieve the output, we have declared an array of objects of all the child classes English, Spanish and French ( to call a class or to retrieve an output from a class we have to create an object of that particular class) and passed the object to out greeting function as the perimeter.


You may also like