Object Oriented Programming and Late Binding with PHP

Technorati Technorati Tags:

RSS Subscription
Posted In:
May 25th, 2009 @ 2:05 am PDT
(0) Comments

Object–oriented programming, OOP, is good for many reasons: Simplifying complex tasks through abstraction layers, preventing data corruption through encapsulation, and enhanced maintainability through modularity. For me, the biggest reason to support the OOP paradigm is that it facilitates code reuse through inheritance and functional diversity through polymorphism. Why rewrite a program core that applies equally well to two problems when it can be reused and augmented as required through inheritance and polymorphism? In short, OOP allows you to truly begin to build a maintainable, useful, and reusable software library.

Fortunately, PHP 5 has made progress since version 4 in its support for OOP. One of the more useful features now supported is late binding (sometimes called dynamic binding) — the ability to invoke a derived classes implementation of a method previously defined, and possibly wrapped by another method, in the base class. Without late binding a transition from a purely general foundation to a more specific, unforseen future implementation would be difficult if not impossible and polymorphism would stop dead in its tracks. With that in mind, I'll demonstrate some of the OOP features in PHP 5.2.6 through a simple late binding example.

First define the base class and a class derived from it to override one of the methods:

<?php
class Base 
{
   private $msg;
   public function __construct() { $this->msg = "Hello"; }
   public function setMsg($s) { $this->msg = $s; }
   public function getMsg() { return $this->msg; }
   //This method will be overriden in the derived class below
   public function message() { return $this->msg . " from base class."; }
   //This method demonstrates late binding, and that PHP 5 understands class-typed parameters
   public function speak(Base $other)
   {
      echo $this->message() . " " . $other->message() . "<br />";
   }
}//End Base Class

class Derived extends Base
{//Override Base class constructor & message() method
   public function __construct() { $this->setMsg("Goodbye"); }
   public function message()
   {
      return $this->getMsg() ." from derived class.";
   }
}//End Class Derived
?>

Now a client of these classes can call the speak() method with the correct implementation of the calling and parameter object's message() method executed, thanks to late binding support. Here is an example.

$b = new Base()
$d = new Derived()
$b->speak($d);
$d->speak($b);

When this code is interpreted the output would be the following:

Hello from base class. Goodbye from derived class.
Goodbye from derived class. Hello from base class.

As you can see, the derived classes implementation of message() executed despite being called from within the speak() method previously defined in the base class, and not redefined anywhere in the derived class. This is a pure example of polymorphism through late binding. Moreover, OOP in PHP is very similar to other object oriented langues such as C++ and Java. It uses visibility through keywords such as public, private, and protected. There is also support for constructors as well as destructors, and a pointer to the class itself through the use of the keyword "this".

To learn more about OOP in PHP 5 you should read the Classes and Objects (PHP 5) documentation.

Share this Post

There are 0 comments for this post.

Add a comment to this post!

(Thanks!)

HTML Allowed (but not necessary): <p>, <a href=' '>*, <em>, <pre>, <code> & <blockquote>.  *rel='nofollow' is set for all <a> tags.