Polymorphism is one of the three main paradigms of the PLO. In short, polymorphism is the ability of an object to use the methods of a derived class that does not exist at the time of creating the base class. For those who are not well versed in the PLO, it probably sounds difficult. Therefore, consider the use of polymorphism by example.
Formulation of the problem
Suppose the site needs three types of publications - news, announcements and articles. In some ways they are similar - they all have a headline and text, news and announcements have a date. In some ways they are different - articles have authors, news have sources, and ads have a date after which it becomes irrelevant.
The simplest options that come to mind are to write three separate classes and work with them. Or write one class in which all the properties inherent in all three types of publications will be, and only the necessary ones will be used. But after all, for different types, similar in logic methods should work in different ways. Doing several methods of the same type for different types (get_news, get_announcements, get_articles) is completely illiterate. This is where polymorphism helps.
Abstract class
Roughly speaking, this is a class template. It implements the functionality only at the level at which it is known at the moment. Derived classes complement it. But, it's time to move from theory to practice. At once I will make a reservation, a primitive example with minimal functionality is considered. All explanations are in the comments in the code.
')
abstract class Publication
{
// ,
protected $table ;
//
protected $properties = array();
//
public function __construct ( $id )
{
// , ,
$result = mysql_query ( 'SELECT * FROM `' . $this -> table . '` WHERE `id`="' . $id . '" LIMIT 1' );
// ,
$this -> properties = mysql_fetch_assoc ( $result );
}
// , ,
public function get_property ( $name )
{
if (isset( $this -> properties [ $name ]))
return $this -> properties [ $name ];
return false ;
}
// , ,
public function set_property ( $name , $value )
{
if (!isset( $this -> properties [ $name ]))
return false ;
$this -> properties [ $name ] = $value ;
return $value ;
}
// , , ,
abstract public function do_print ();
}
Derived classes
Now you can proceed to the creation of derived classes that implement the missing functionality.
class News extends Publication
{
// ,
public function __construct ( $id )
{
// ,
$this -> table = 'news_table' ;
//
parent :: __construct ( $id );
}
//
public function do_print ()
{
echo $this -> properties [ 'title' ];
echo '<br /><br />' ;
echo $this -> properties [ 'text' ];
echo '<br />: ' . $this -> properties [ 'source' ];
}
}
class Announcement extends Publication
{
// ,
public function __construct ( $id )
{
// ,
$this -> table = 'announcements_table' ;
//
parent :: __construct ( $id );
}
//
public function do_print ()
{
echo $this -> properties [ 'title' ];
echo '<br />! ' . $this -> properties [ 'end_date' ];
echo '<br /><br />' . $this -> properties [ 'text' ];
}
}
class Article extends Publication
{
// ,
public function __construct ( $id )
{
// ,
$this -> table = 'articles_table' ;
//
parent :: __construct ( $id );
}
//
public function do_print ()
{
echo $this -> properties [ 'title' ];
echo '<br /><br />' ;
echo $this -> properties [ 'text' ];
echo '<br />© ' . $this -> properties [ 'author' ];
}
}
Now about using
The bottom line is that the same code is used for objects of different classes.
// , Publication
$publications [] = new News ( $news_id );
$publications [] = new Announcement ( $announcement_id );
$publications [] = new Article ( $article_id );
foreach ( $publications as $publication ) {
// Publication
if ( $publication instanceof Publication ) {
//
$publication -> do_print ();
} else {
//
}
}
That's all. With a flick of the wrist, the pants turn into elegant shorts :-).
The main benefit of polymorphism is the ease with which you can create new classes that “behave” in a similar way, which, in turn, allows us to achieve extensibility and modifiability. The article shows only a primitive example, but even it shows how the use of abstractions can facilitate development. We can work with the news exactly as with ads or articles, and we don’t even have to know what we are working with! In real, much more complex applications, this benefit is even more tangible.
A bit of theory
- Methods that require overriding are called abstract. It is logical that if a class contains at least one abstract method, then it is also abstract.
- Obviously, it is impossible to create an object of an abstract class, otherwise it would not be abstract.
- The derived class has properties and methods that belong to the base class, and, in addition, it can have its own methods and properties.
- A method that is redefined in a derived class is called virtual. There is no information about this method in the base abstract class.
- The essence of abstraction is to define a method in the place where there is the most complete information about how it should work.
UPD: about sql-inj and MVC violation - gentlemen, this is just an example, and an example on polymorphism, in which I do not consider it necessary to pay attention to these things. This is a topic for very different articles.
Original on my site