Class Polymorphism in PHP

Lecture



  Class Polymorphism in PHP

Polymorphism (multiformity) is a consequence of the idea of ​​inheritance. In general terms, class polymorphism is a property of the base class to use the functions of derived classes, even if at the time of the definition it is not yet known which class will include it as the base class and thus become derived from it.

Consider the polymorphism property of classes based on the following example:

<?php
class A {
// Выводит, функция какого класса была вызвана
function Test ()   { echo "Test from A\n" ;   }
// Тестовая функция — просто переадресует на Test()
function Call ()   { Test ();   }
}
class B extends A {
// Функция Test() для класса B
function Test ()   { echo "Test from B\n" ;   }
}
$ a = new A ();
$ b = new B ();
?>

Use the following commands:

$a->Call(); // выводит "Test from A"
$b->Test(); // выводит "Test from B"
$b->Call(); // Внимание! Выводит "Test from B"!

Pay attention to the last line: contrary to expectations, it is not the Test () function from class A that is called, but a function from class B ! It seems that Test () from B simply redefined the Test () function from A. So it really is. A function that is redefined in a derived class is called virtual .

The mechanism of virtual functions allows, for example, to “slip” functions that are waiting for an object of one class, an object of another, derived, class. Another classic example is a class that embodies the properties of a geometric figure, and several classes derived from it - a square, a circle, a triangle, etc.
The base class has a virtual function Draw () , which causes the object to draw itself. All derived shape classes, of course, override this function (after all, each shape needs to be drawn in a special way). We also have an array of figures, and we don’t know which ones. But using polymorphism , we can, without thinking, sort through all the elements of the array and call Draw () for each of them - the figure itself decides what type it is and how to draw it.

And here is another practical example showing the class property - polymorphism :

<? php
class Base {
  function funct ()   {
echo "<h2>Функция базового класса</h2>" ;
  }
  function base_funct ()   {
  $ this -> funct ();
  }
}

class Derivative extends Base {
  function funct ()   {
echo "<h3>Функция производного класса</h3>" ;
  }
}

$ b = new Base ();
$ d = new Derivative ();

$ b -> base_funct ();
$ d -> funct ();
$ d -> base_funct ();
// Скрипт выводит:

// Функция базового класса
// Функция производного класса
// Функция производного класса
?>

In the above example, the base_funct () function of the Base class was overwritten by the same function of the Derivative class.


Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Running server side scripts using PHP as an example (LAMP)

Terms: Running server side scripts using PHP as an example (LAMP)