developer.com
Search EarthWeb
CodeGuru | Gamelan | Jars | Wireless | Discussions
Navigate developer.com
Architecture & Design  
Database  
Java
Languages & Tools
Microsoft & .NET
Open Source  
Project Management  
Security  
Techniques  
Voice  
Web Services  
Wireless/Mobile
XML  
New
 
Technology Jobs  

   Developer.com Webcasts:
  The Impact of Coding Standards and Code Reviews

  Project Management for the Developer

  Defining Your Own Software Development Methodology

  more Webcasts...




See The Winners!




Developer Jobs

Be a Commerce Partner














 


Developer News -
Sun Releases NetBeans 6.7 IDE for Java, PHP    June 29, 2009
Why Firefox Doesn't Take Google Chrome Features    June 26, 2009
First Major PHP Update in Years Coming Soon    June 25, 2009
Red Hat CEO Calls on Oracle to Keep Java Open    June 25, 2009
Free Tech Newsletter -

The Object Oriented Improvements of PHP 5
By W. Jason Gilmore

Go to page: 1  2  3  Next  

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.

Go to page: 1  2  3  Next  

Next article: A Look at PHP 5, Part II


Tools:
Add www.developer.com to your favorites
Add www.developer.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed


PHP Archives






internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs