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:
<?phpclass 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 belowpublic function message() { return $this->msg . " from base class."; }//This method demonstrates late binding, and that PHP 5 understands class-typed parameterspublic function speak(Base $other){echo $this->message() . " " . $other->message() . "<br />";}}//End Base Classclass Derived extends Base{//Override Base class constructor & message() methodpublic 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.
Technorati





