Understand polymorphism correctly

Hello everyone Help us understand polymorphism correctly. Since there are many examples on the Internet and they all differ from each other.

As I understand it. This is when a property of the base class can use methods of derived classes.

I often see two examples of polymorphism on the Internet

Example 1.

class user {
    public $type = 'default_user';
    public function setName (){
    }
    public function Call (){
          return $this->setName();
    }
}
class admin extends user {
     public function setName (){
         return $this->type = "admin";
     }
}
class superUsers extends user {
    public function setName (){
        return $this->type = "superUser";
    }
}
$super = new superUsers;
$admin = new admin;
echo $super->call();
echo $admin->call();

Example 2.

class user {
    public $type = 'default_user';
    public function setName (){
    }
}
class admin extends user {
     public function setName (){
         return $this->type = "admin";
     }
}
class superUsers extends user {
    public function setName (){
        return $this->type = "superUser";
    }
}
$super = new superUsers;
$admin = new admin;
echo $super->setName();
echo $admin->setName();

Since I understood both in the first example and in the second example, polymorphism is shown , and this type is called override.

Author: mzd, 2016-12-20

1 answers

It is worth starting with the fact that polymorphism is different.

In OOP, polymorphism is most often referred to as the ability of classes with the same specification (interface) to define a different implementation, which, in turn, allows the client code to abstract from this very implementation and work with the class based on its specification.

For example, your method might expect to receive an object of type UserInterface as input, without knowing which specific subtype of type UserInterface it will be associated with. work. In this way, you can uniformly process different data types, relying on each input parameter to conform to the UserInterface specification.

At the same time, some OOP languages use the so-called "duck typing". It's implicit typing. This is when the client code expects that the object it is using has some method defined. This allows you to use polymorphism to handle objects that aren't even necessarily in the hierarchy. inheritance.

For example, if I write a function:

function ($object) {
    echo $object->setName();
} 

It will be able to work correctly with your inheritors from user, as well as with any other object that can call the setName method without parameters, which would return a string. For example:

class DefinitelyNotAUser {

    public function setName()
    {
        return 'haduken!';
    }

}

The anonymous function described above will be able to work with instances of this class in the same way as with your heirs from user.

This is called signature polymorphism. This approach is widely used it is common in Ruby, but I would not recommend using it in PHP.

A function implemented for an arbitrary data type will also be an example of polymorphism (parametric). For example:

function handler($object, callable $action)
{
    return $action($object);
}

$result = handler(new admin(), function ($object) { echo $object->setName(); });

It is worth noting that polymorphism is not controlled by something peculiar exclusively to the object-oriented paradigm. Polymorphism is present in both the functional and procedural paradigms. Parametric polymorphism of a function described for an arbitrary data type is an example for an OP. Challenge the specific implementation of a function, depending on the types of parameters passed, is an example of polymorphism for procedural programming.

The main manifestation of polymorphism is the late binding of the called code to the caller, when the startup environment is able to determine which implementation is called, based on the context of the program execution.

The simplest definition of polymorphism, which you now need to remember, given that the tags to the question are PHP and OOP: "One interface - many implementations."

What is wrong in your current understanding: you are fixated on the relations of the base and derived classes. Polymorphism is not about that. It is about the calling (client) code and the called code, as well as their dynamic correspondence.

 7
Author: Igor Karpenko, 2016-12-20 16:49:59