July 24, 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 »

Protected Methods

Like protected members, protected methods are accessible both within the class in which they are declared, as well as by all subclasses.

Static Methods

Most applications make use of various functions of use throughout the entire application. Because such functions aren't necessarily related to any particular object, they're often placed in a general utility class. However, such a strategy is followed because it's good OO programming practice, and not because we want to invoke a "utility" object (although you could). Rather, we just want to call the method as necessary, while still managing to encapsulate it in some sort of class. Class methods that can be called without first instantiating an object are known as static. Let's consider an example:

<?php
   class logger {

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

   }

   echo logger::getCredits();
?>

Note that you cannot reference the $this keyword within static methods, because there was no prior object instantiation.

Abstract Methods

An abstract method is intended as a cue for any developer interested in implementing a specific class. While an abstract method doesn't define the method's implementation, it acts as a guide to the developer, informing him that he will have correctly implemented the class provided that it and all other abstract methods found in the class are implemented. An example is provided in the next section.

Abstract Classes

The object-oriented development strategy is particularly appealing because we easily relate to it. Humans are quite adept at thinking in terms of hierarchical structures. For example, its obvious to all of us that compact discs, MP3s and MPEGs all fall under the category of media. However, media is solely an abstract concept which we use to generalize a group of items that share a set of common characteristics which we collectively define as media.

Its often convenient for programmers to embody these shared characteristics and behaviors in an abstract class. Class abstraction affords the programmer the convenience of wielding control over a core set of properties and behaviors that he knows will be shared by a number of similar, yet unique classes. While an abstract class is never directly instantiated, any class which inherits it will also possess its properties and behaviors.

PHP 5 supports class abstraction. As an example, let's create the media class. This class holds one property, $copyrightInfo, one public function, setCopyrightInfo(), and one abstract function, play(). While this class in useless by itself, it can readily be inherited by any of our specific media types.

<?php
   abstract class media {

      private $copyrightInfo;

      abstract function play();

      function setCopyrightInfo($info) {
         $this->copyrightInfo = $info;
      }

   }

   class cd inherits media {
      function play() {
          echo "The CD has started playing!";
      }
   }

   $cd = new cd();
   $cd->setCopyrightInfo("Copyrighted by who else? Me!");
   $cd->play();

?>

Interfaces

Interfaces bring an additional level of both convenience and discipline to object-oriented programming by providing a means for blueprinting an application component, defining the methods which together make that component what it is. A class is said to implement an interface provided that it satisfies all of its defining characteristics.

To the great pleasure of many developers, PHP 5 offers interfaces! Let's consider an example:

<?php

   interface fighter {
      function power();
      function weight();
   }

   class boxer implements fighter {

      private $power;
      private $weight;

      function power() {
         return $this->power;
      }

      function weight() {
         return $this->weight;
      }

}
?>

A class can also simultaneously implement multiple interfaces. Let's consider another example:

<?php

   interface fighter {
      function power();
      function weight();
   }

   interface kickboxer {
      function kick();
   }

   class warrior implements fighter, kickboxer {

      private $power;
      private $weight;

      function power() {
         return $this->power;
      }

      function weight() {
         return $this->weight;
      }

      function kick() {
         return "kick!";
      }

}

?>

Failing to implement all of the functions as defined within an interface results in an error like so:

Fatal error: Class warrior contains 1 abstract methods and must therefore be declared abstract (kickboxer::headbutt) in /www/htdocs/developer/interface.php on line 30

Unified Constructors

While PHP 4 offered constructors, the syntax required that the constructor method be the same name as the class. PHP 5 resolves this inconvenience by providing a standardized constructor syntax, using the name __construct(). For example:

<?php

   class blog {

      private $title;

      function __construct() {
         $this->title = "Just another rant";
         echo "New blog object created!;
      }
   }

   $blogobj = new blog();
?>

Executing this code results in the assignment of the $title member, and output of the following message:

New blog object created!





Page 2 of 3



Comment and Contribute

 


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

 

 


Sitemap | Contact Us

Rocket Fuel