📜 ⬆️ ⬇️

What polymorphism really is. It also exists in PHP.

Write this post prompted a recent article on polymorphism . It caused a lot of controversy, but knowledgeable people understand that the author wrote more about inheritance and redefining methods than about polymorphism. I will not say either good or bad about that article, but just tell you what polymorphism really is. So that novice php-programmers do not remain in error.

The simplest thing is to characterize the paradigm of polymorphism as the interchangeability of objects with the same interface, as can be seen in Wikipedia .

So, what is the basis of polymorphism.
We have a class or interface (hereinafter the interface) that will be a framework, an add-on, etc. This interface will determine the general behavior of all its heirs, i.e. what will be called their main actions (methods). Well, the heirs, in turn, can override these methods, i.e. set the individual behavior of these methods. This part was presented in the article.
s_a_p'a . But this is only the basis of polymorphism. Its very essence lies in the fact that we guarantee to ourselves that all methods of the interface will behave “correctly” no matter what specific derived class is used. Perhaps now it is not entirely clear, but below I will give an example where the situation will become clearer.

I will not go directly into polymorphism itself and will not write about time polymorphism and compilation. We will be closer to the topic of this blog, namely to PHP.
')
PHP Polymorphism

The PHP language supports polymorphism in the sense that it allows the use of subclass instances instead of instances of the parent class. The commissioning of the required method is carried out at the stage of the run. There is no support for method overloading, in which the method is put into action taking into account the method signature. The fact is that in each class there can be only one method with a certain name. But due to the fact that the PHP language uses weak typing and supports a variable number of parameters, it is possible to get around this limitation. If you are interested, I can share knowledge.

PHP polymorphism example

Let's take as a basis the names of the classes of the article already mentioned.
In this case, our interface will be an abstract class.
abstract class Publication {
// define the rule that all publications should be printed, i.e. have do_print () method
abstract public function do_print ();
}

class News extends Publication {
// redefine abstract printing method
public function do_print () {
echo '<h4> News </ h4>' ;
// ...
}
}
class Announcement extends Publication {
// redefine abstract printing method
public function do_print () {
echo '<h4> Ad </ h4>' ;
// ...
}
}
class Article extends Publication {
// redefine abstract printing method
public function do_print () {
echo '<h4> Article </ h4>' ;
// ...
}
}

// Fill the array of publications with objects derived from Publication
$ publications [] = new News ();
$ publications [] = new Announcement ();
$ publications [] = new Article ();

foreach ($ publications as $ publication) {
if ($ publication instanceof Publication) { // If we work with the heirs of Publication
$ publication-> do_print (); // then we can safely print the data
}
else {
// exception or error handling
}
}


The main thing here is the last part. That is the essence of polymorphism. We use the same code for objects of different classes.
Now how it can be applied in practice. Suppose a user wants to keep track of the latest updates of publications, and it does not matter to him whether there will be articles or news or something else.
Taking into account the fact that the tables are different, the fields in them are also different, it is most convenient to get separate lists for news and for articles. But we can not display them separately - the user is interested in the time sequence. Therefore, we sort the data by forming one large array of objects. Well, then, as in the example above, we can display all objects within the framework of a single interface. But at the same time on the screen news and articles will be displayed a little differently, for example with a different icon or a different beginning of the title. Thus, we do not need to do any checks on what we work with. The interpreter will do it for us.

I hope now the topic of polymorphism has become more understandable. As noted above, if it is interesting, I can write about the redefinition of methods, in which the input of the method into action takes into account the signature. Contact us.

Source: https://habr.com/ru/post/37610/


All Articles