Why use Polymorphism at all in PHP? Trying to understand the benefit

So my question is this then… What is the benefit of Polymorphism against just a general class implementation. EG: Why can’t I just do this:



    <?php 
    
    
    class circle {
    
    
    public function calcArea($radius) {
    
    
    return 3.14 * $radius * $radius;
    
    
    }
    }
    
    
    class square  {
    
    
    public function calcArea($width, $height) {
    
    
    return $width * $height ;
    
    
    }
    }

Or even why not this:

    class calculations {
    
    
    public function calcCircleArea($radius) {
    
    
    
    return 3.14 * $radius * $radius;
    
    
    
    }
    
    
    public function calcSquareArea($width, $height) {
    
    
    
    return $width * $height ;
    
    
    
    }

Instead of this:


<?php
interface Shape {
   public function getArea();
}

class Square implements Shape {
   private $width;
   private $height;

   public function __construct($width, $height) {
      $this->width = $width;
      $this->height = $height;
   }

   public function getArea(){
      return $this->width * $this->height;
   }
}

class Circle implements Shape {
   private $radius;

   public function __construct($radius) {
      $this->radius = $radius;
   }

   public function getArea(){

      return 3.14 * $this->radius * $this->radius;
   }
}

function calculateArea(Shape $shape) {
   return $shape->getArea();
}

$square = new Square(5, 5);
$circle = new Circle(7);

echo calculateArea($square), "<br/>";
echo calculateArea($circle);
?>

In both cases the code is clean looking, has less coding and it doesn’t seem to affect memory or speed of execution. So why would we use polymorphism in this case? I am just trying to understand what makes it the way to go. Thanks!

Well let’s first acknowledge that your use case is an extremely simplistic one designed to show the mechanic, rather than be a demonstration of top reward for use.

I just used the example I see used the most in the tutorial. Perhaps that is the issue then… The tutorials are using this simple design that I am not seeing the big picture?

It ties into the open/closed principle which (simplified) states that software must be open to extension but closed for modification.

To keep with your example, if you need to add another shape, let’s say an hexagon, you just add the class and nothing else needs to be changed / added, since they already know how to work with shapes.

Basically it allows for very localised changes, instead having to change a gazillion layers when you add/change an object.

1 Like

See C++ Polymorphism. That explains real polymorphism as in C++. I will simplify what is there to make it relevant here. See the following:

class funcOl {
public:
      funcOl ();
      funcOl (int i);
      int add(int a, int b);
      int add(float a, float b);
};

Note that there are two add functions; one takes integers and the other takes floating-point. I hope you understand that; it is not how PHP works, at least not classical PHP. So for C++ we can call add for the same class but with different parameter types.

Now see oop - Is what seems like polymorphism in PHP really polymorphism? - Stack Overflow. It is not clear if PHP supports polymorphism. The explanations are technical and perhaps I or someone will answer specific questions about that but I hope that Stackoverflow thread makes it clear that it is not clear whether PHP supports polymorphism.

Yes, I can alter the interface to add more shapes. But why add it to an interface when I can just add it as a separate class? I mean, I am doing that already anyway. Adding the interface just seems to be extra unnecessary work, especially since I have to add a construct. I feel like I am missing something here… But this seems to be a highly urged method of programming for PHP and I just do not understand it’s purpose.

Let’s take another example that maybe makes more sense.

Let’s say you’re building a webshop from scratch and it accepts two payment methods, let’s say PayPal and Apple Pay.

Now, what you could do is just create two classes, one for each payment method, and then code knowledge about how these classes work all over the place. Or, you could create a PaymentMethodInterface that describes what we expect of a payment method and let the PayPal and Apple Pay classes implement it. From that point you can pass these payment methods around without the classes that are using them knowing which implementation they’re dealing with, they just know they’re dealing with some payment method, and that’s enough.

The added benefit is that once you add another payment method, let’s say Stripe, you add the class, let it implement the interface, and the rest of your code can use it no problem. It doesn’t care it’s not PayPal or Apple Pay. It cares that it implements the PaymentMethodInterface, and it does.

As soon as you pass some other class though that doesn’t implement the PaymentMethodInterface, like your Circle class, it will complain, because that can’t be used to make payments.

What it boils down to is that interfaces let you code to the concept of things (like “a payment method”), rather than the implementation of things (PayPal, Apple Pay, Stripe,…).

2 Likes

But couldn’t this be done by simply using inheritance in the class rather than Extends (Sorry, I said implements earlier)? Sorry, I am not trying to nitpick. I am just trying to understand why I should gain knowledge of this further rather than just using what I am already doing. I appreciate all your responses.

I don’t understand the question. Extends = inheritance.

Because if you use classes you just bound to some class or class hierarchy. Interface is independent of concrete implementation.

I think that is not polymorphism. That does however describe a typical use of virtual methods. As I said, PHP does not do polymorphism in any manner that is clearly polymorphism. Since polymorphism in PHP must be done using multiple classes/interfaces it is likely it is not clear what is polymorphism in PHP. My understanding is that polymorphism is doing the same things except using varying data types.

I think multiple payment methods would not be polymorphism since it would be implementing multiple methods (likely with the same parameters) that are called the same.

Well, according to Wikipedia (https://en.m.wikipedia.org/wiki/Polymorphism_(computer_science)), there are several types of Polymorphism. The one discussed in this tread is subtyping.

The one you seem to be talking about is ad-hoc Polymorphism (aka method overloading). Indeed, PHP does not support that (sadly), so there’s no point talking about that in a PHP thread.

Subtyping however does exist in PHP so we can keep discussing that part.

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.