Polymorphism in PHP Object-oriented programming Examples

Today, We want to share with you polymorphism in php.In this post we will show you Run time, hear for compile time polymorphism in php we will give you demo and example for implement.In this post, we will learn about Encapsulation using PHP OOP with an example.

How To Implement Polymorphism In PHP?

In PHP Polymorphism represent an example in OOP means object-oriented programming where methods in different types of the classes that do equivalent things should have a equivalent name.

  • Poly meaning => many
  • morphism meaning forms

There are main two types of Polymorphism in PHP like as a

  1. polymorphism in Compile time (function overloading)
  2. polymorphism in Run time (function overriding)

Imp Note for: PHP Interview Questions

So, PHP compile time polymorphism : “does not support”

Polymorphism in PHP

This word is can from mainly use of the Greek word poly and morphism. Poly stands lots of or “many” as well as morphism stands some property which uses us to give more than one property. like as a concept of the => Overloading Same PHP method name with different signature type, since PHP doesn’t support PHP method Data overloading concept like as a => Overriding When same methods defined in main as well as sub class with same data signature Example as a know as PHP method overriding

So let’s implement step by step learn all about the polymorphism principle example demo with the use of the interface bellow.

PHP – basic concept of Polymorphism

";  
    }  
}  

class Brand extends Module  
{  
    function produce()  
    {  
     print "Brand has been generated.
"; } } class Category extends Module { function produce() { print "Category has been generated."; } } $results=array(2); $results[0]=new Product(); $results[1]=new Brand(); $results[2]=new Category(); for($i=0;$i<3;$i++) { $results[$i]->produce(); } ?>

Explain Polymorphism in PHP.

 unit = $unit;
      }
      public function totalWork(){
         return $this -> unit * $this -> unit * pi();
      }
   }
   class Learndept implements Module {
      private $time;
      private $focus;
      public function __construct($time, $focus){
         $this -> time = $time;
         $this -> focus = $focus;
      }
      public function totalWork(){
         return $this -> time * $this -> focus;
      }
   }
   $liveteach = new TechDept(3);
   $livelearn = new Learndept(3,4);
   echo $liveteach->totalWork();
   echo $livelearn->totalWork();
?>

Output:

28.274
12

Example Explanation:

The interface with the name of ” Module” commits all the classes that implement it to define an abstract method with the name of totalWork(). In accordance, the TechDept class implements the interface by defining the callWork() method with the respective body inside it. The learndept class also implements the Module interface but defines the method totalWork() with a various body that differs from the techDept class CalWork() method. The polymorphism guideline says that, for this situation, all the methods that calculate the work would have the equivalent name. Now, at whatever point I would required to compute the Work for the various Classes, I would call a method with the name of totalWork() without giving a lot of consideration to the details of how to actually calculate the Work for the various Module. The main thing that I would required to know is the name of the method that calculates the Work.

Method Overriding

included(4047,698);
 
?>

Conclusion

In this Big Post, I have learned how to implement step by step the polymorphism principle in PHP. Click here to practice the PHP subject. In the next Big Post, I will learn about type Exercise programs that can restrict our all very useful Polymorphism functions and as well as methods to use only arrays or objects that were created from a individual some class.

I hope you get an idea about polymorphism in php.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment