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 -
Mozilla's Ubquity Mashup: For The Masses?    August 27, 2008
iPhone Users Just Want to Have Fun    August 26, 2008
Oops! I Fixed the Linux Kernel    August 22, 2008
Jim Zemlin: The New Center of Linux Gravity    August 21, 2008
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








JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Intel PDF: Virtualization Delivers Data Center Efficiency
Intel eBook: Managing the Evolving Data Center
Microsoft Article: BitLocker Brings Encryption to Windows Server 2008
Symantec eBook: The Guide to E-Mail Archiving and Management
Microsoft Article: RODCs Transform Branch Office Security
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
Avaya Article: Advancing the State of the Art in Customer Service
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Avaya Article: Avaya AE Services Provide Rapid Telephony Integration with Facebook
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Seminar: Efficiencies in Hardware/Software Virtualization
HP Webcast: Disaster Recovery Planning
Go Parallel Video: Performance and Threading Tools for Game Developers
HP Video: StorageWorks EVA4400 and Oracle
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
IBM TCO eKIT: Your IT Budget is Under Attack, Get in Control
IBM Energy Efficiency eKIT: Learn How to Reduce Costs
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt and free High-Performance SQL Code eBook
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
Microsoft Article: Silverlight Streaming--Free Video Hosting for All
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
HP Demo: StorageWorks EVA4400
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES