Thursday, 23 August 2018

Getting to know Polymorphism and its practice in PHP

Polymorphism is a concept in OOP where Classes are
have different functionality from each other but at the same time apply the same Interface. For those of you who learn OOP in PHP it might be a little confused ... but we will give an example and an easy analogy so you understand.

For example, we have an Android, iPhone, Blackberry ... they each have unique and different ways of working but they also actually have at least 1 function / interface that is the same as for example Vendors. So both Android, iPhone, Blackberry all have the same Vendor ... but whoever the vendor is of course this can be different from each other.

If we write in the Code like this

interface {

   public function vendor ();

}

Android class {

   public function vendor () {/ * samsung * /}

}

class Iphone {

   public function vendor () {/ * Apple * /}

}

class Blackberry {

   public function vendor () {/ * RIM * /}

}
Polymorphism is implemented in OOP with the help of Interface features, Abstract Class and Factory Pattern as a complement. We can choose whether to use Interface or Abstract Class, but we must first understand what Interface and Abstract Class are and when each must be used.

Problem Interface, Abstract Our class will not be discussed here. Friends can Googling themselves.

Understanding of Polymorphism and the example of the above Code has been understood? if we just practice right away.

Polymorphism with Interface

Case study: A campus requires a simple administrative system that can generate / generate a Master Number for its Students, Lecturers and Employees.

At a minimum, there are 2 simple ways for the above case, first, it makes the Conditional Statement function simple or the second one completes with the concept of Polymorphism.

With Conditional Statement we can make it like this

function numberInduk ($ category = '') {

   $ number = '';

   switch ($ category) {
      'student' case:
         $ number = 'MHS'. mt_rand (100, 1000);
         break;
      case 'lecturer':
         $ number = 'DSN'. mt_rand (100, 1000);
         break;
      case 'employee':
        $ number = 'KRY' mt_rand (100, 1000);
        break;
   }

   return $ number;
}

echo numberInduk ('student');
echo numberInduk ('lecturer');
echo numberInduk ('employee');
With a simple function / method as above, the case is correct. The only problem is what happens if we want to add another category or want to change the numbering algorithm for each category?

Of course we will change 1 function or class above ... then it can cause new problems like Bugs etc. So the ideal solution is that we have to refactor the above solution into a more modular solution using Polymorphism.

By using Polymorphism + Interface, we can make it like this.

Administration interface
{
    public function number Main ();
}

class Students implements Administration
{
    protected $ prefix = 'MHS';

    public function numberInduk ()
    {
       return $ this-> prefix. mt_rand (100, 1000);
    }
}

class Lecturer implements Administration
{
    protected $ prefix = 'DSN';

    public function numberInduk ()
    {
       return $ this-> prefix. mt_rand (100, 1000);
    }
}

Employee class implements Administration
{
    protected $ prefix = 'KRY';

    public function numberInduk ()
    {
       return $ this-> prefix. mt_rand (100, 1000);
    }
}

// we continue to make caller functions

Main Number generate function (Administration $ adm) {
   return $ adm-> numberBand ();
}


echo generate Number Number (new Student ());
echo generateNomorInduk (new Lecturer ());
echo generate Number Number (new Employee ());
It appears that Polymorphism makes our applications more modular. In practice ... the above classes will be separated (one class 1 file)

For example to be:


- AdministrasiInterface.php
- Mahasiswa.php
- Dosen.php
- Karyawan.php

That way any changes to the application or in the class become easier without having to interfere with the others

Polymorphism with Abstract Class

With the same case as the example above

Administration abstract class
{
     private $ name;

     public function setName ($ name) {
         $ this-> name = $ name;
     }

     abstract public function numberInduk (); // pay attention ...
}

Student class extends Administration
{
     protected $ prefix = 'MHS';

     public function numberInduk ()
     {
          return $ this-> prefix. mt_rand (100, 1000);
     }
}


// ...

// we continue to make caller functions

Main Number generate function (Administration $ adm) {
     return $ adm-> numberBand ();
}


echo generate Number Number (new Student ());

// ...
Another example: Usually PHP Framework and others use the concept of Polymorphism for the Query Builder


abstract class DB
{
     abstract protected function select ($ table, $ column);
}

Mysql class extends DB
{
    public function select ($ table, $ column)
    {

       // ...
    }

}

Oracle extends DB class
{
    public function select ($ table, $ column)
    {

       // ...
     }

}

// Factory class
Query class
{
     public static function driver ($ driver)
     {
        switch ($ driver) {
           case 'mysql':
             return new Mysql ();
             break;
           case 'oracle':
             return new Oracle ();
             break;
           default:
             return new Mysql ();
             break;
       }
    }
}

// For example the use
$ query = Query :: driver ('mysql'); // replace for example ('oracle')

$ query-> select ('student', array ('id', 'name', 'address'));
The conclusion is that implementing Polymorphism helps our program structure be more modular and easier when there are changes or additions in the future.

If you have questions, please comment below ...

Share this


0 Comments