PHP: Interfaces and Abstract Classes

An interface in PHP could be thought of as a very abstract object that defines mandatory procedures without specifying what those procedures should do. The characteristic of an interface is that if a procedure is defined, it must be implemented in the class. Interfaces can be combined - i.e. multiple interfaces can be specified - and can even be extended.

For the most part, the abbreviation OOP is understood to mean using classes - which also serves as an isolator for functions and variables. The "Holy Grail" of OOP and programming in general is the ability to reuse code written once, and approaching this Holy Grail could be symbolically described as the ability to use code fragments such that implementing an individual task requires only "tweaking a few lines". In principle, in OOP this can be achieved through object (class) extension (extends).

class Animal {
  public function feed() {
    echo "eats grass";
  }
}

class Lion extends Animal {
  public function breathe() {
    echo "breathes";
  }
}

The class Lion thus inherits the function feed() and defines a new breathe().

Some problems may arise:

1. If an object (class) or function is written by different programmers, how do you arrive at a unified standard for naming functions (procedures) and what functions must exist at all?
2. What should the set of mandatory functions be?
3. How to prevent and handle situations where, after repeatedly extending an object, it becomes unclear which object's function is being executed?

Interfaces and Abstract Classes.

An interface, as one might think, is some kind of toolset designed to create a connection with or between an object. One might mistakenly assume that interfaces are connections to external objects - this is not the case.
Reading the PHP documentation, the advantages of using interfaces may also not be immediately clear. So I'll try to explain how I have understood it myself.
In principle, an interface in PHP could be thought of as a very abstract object that defines mandatory procedures without specifying what those procedures should do. The characteristic of an interface is that if a procedure is defined, it must be described in the class. Interfaces can be combined - i.e. multiple interfaces can be specified - and can even be extended.

Some examples:

#1.
interface PageInterface
{
  public function render();
}

#2a.
class Login implements PageInterface
{
  public function render()
  {
   return 'Render Login Page';
  }
}

#2b.
class Register implements PageInterface
{
  public function render()
  {
   return 'Render Register Page';
  }
}

#3.
class PageCollection
{
  private $pPage = array();

  public function add(PageInterface $pPage)
  {
   $this->m_aPageCollection[] = $pPage;
  }

  public function renderPages()
  {
   foreach ($this->m_aPageCollection as $pPage)
   {
     echo $pPage->render();
   }
  }
}

#4.
$pPageCollection = new PageCollection();
$pPageCollection->add(new Login());
$pPageCollection->add(new Register());
$pPageCollection->renderPages(); 

Combining interfaces:

#5.
interface AdminPageInterface
{
  public function auth();
}

class AdminNews implements PageInterface, AdminPageInterface
{
  public function auth()
  {
    echo "Auth Code";
  }

  public function render()
  {
    echo "Render Code";
  }
} 


In all objects (classes) that use the PageInterface interface, the function render() must be present. As we can see in illustrations #3 and #4, the mandatory use of the render() function means that the PageCollection object can be made very universal, regardless of the fact that the Login (#2a.) and Register (#2b.) objects may be written by different people.

Abstract classes are a slightly more versatile construct. They work similarly to interfaces, but it is also possible to define functions with code.

Of course, purists might object that interfaces and abstract classes work differently - but I hope I have introduced some clarity on how interfaces can be used. :)

http://www.killerphp.com/articles/php-interfaces/
http://www.talkphp.com/advanced-php-programming/1446-working-interfaces.html

Share:
Rate: 5 (1)
Views:

comments



What are others reading?