August 23, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

The Object Oriented Improvements of PHP 5

  • January 22, 2004
  • By W. Jason Gilmore
  • Send Email »
  • More Articles »

While PHP 4 was the first version to offer Object-Oriented Programming (OOP) capabilities, many considered the feature to be poorly designed and more an afterthought than anything else. PHP 5 resolves many of the version 4 OOP inconsistencies, not to mention greatly enhances the language's object-oriented capabilities. While you're still free to program using a procedural methodology, those of you with object oriented backgrounds will certainly feel much more at home creating fully object-driven PHP applications. In this article I'll introduce many of these new and improved features. In addition, for those of you somewhat new to the subject, this article will also offer a bit of instruction regarding those concepts key to truly understanding OOP.

Private and Protected Members

Object-oriented programming is largely about the ability to hide what's not important to the user and to highlight what is. One of the primary means for doing so is through defining scope for class properties. PHP 4 offered no standardized means for specifying the variety of property scopes typically offered by full-featured OO languages. PHP 5 remedies this in part through support for two common scopes:

Private: Private members are accessible within the class in which they are created. Note that they are not accessible by any subclass!

Protected: Protected members are accessible within the class in which they are created, as well as by any subclass.

In the following example, we'll create a class which contains both a private and a protected member.

<?php
   class boxer {
      // Not available to streetfighter class
      private $age;

      // Available to streetfighter class
      protected $wins;

      function __construct() {
         $this->age = 15;
      }

      // Return boxer's wins
      function getWins() {
         return $this->wins;
      }

      // Return boxer's age
      function getAge() {
         return $this->age;
      }
 
   }

   // Extend the boxer class
   class streetfighter extends boxer {

      protected $teeth;

      function __construct() {
         // This works
         $this->wins = 12;
         // This does not
         $this->age = 30;
      }

   }

   $sf = new streetfighter();
   echo $sf->getWins();

?>

One oddity regarding misuse of private members is the lack of any error message. Rather, attempts to set or get them will simply be ignored.

Note that any member not assigned a scope of private or protected defaults to the public scope, meaning that it's directly accessible from the caller! Because such practice generally goes against the goals of OOP, its advised that you designate such members as private and manage them through getters and setters (otherwise known as properties). Unfortunately, a unified getter/setter methodology has yet to be worked into the language, leaving you to your own devices to come up with a method-based solution.

Final, Private, Protected and Static Methods

Like members, it's often useful to set various levels of scope for methods. PHP 5 defines four previously unavailable levels: final, private, protected and static. I'll discuss each in this section.

Final Methods

While methods which have been declared as final can be used within subclasses, but cannot be overridden. Let's consider an example:

<?php
class logger {

   final function getCredits() {
      return "Copyright 2004 you know who.";
   }

}

class myLogger extends logger {

   function getCredits() {
      return "Give credit where credit is due!";
   }

}

$mylog = new myLogger();
$mylog->getCredits();

?>

Executing this example results in the following fatal error:

Fatal error: Cannot override final method logger::getCredits() in /www/htdocs/developer/finalmethod.php

Private Methods

Like private members, private methods are only accessible within the class in which they are declared. An example follows:

<?php
   class boxer {

      protected $bouts;
      protected $wins;
      protected $losses;

      private function winPercentage() {
         return ($wins / $bouts) * 100;
      }

   }

   class streetfighter extends boxer {

      function __construct() {
         $this->bouts = 10;
         $this->wins = 5;
      }

      function stats() {
         return $this->winPercentage();
      }

   }
?>

Like private members, attempts to access private methods from outside of the originating class will simply result in no action, rather than any error message.





Page 1 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel