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...




Vote for the Developer.com Product of the Year Winners!




Developer Jobs

Be a Commerce Partner














 


Developer News -
Red Hat Fedora Claims It's the Leader in Linux    November 21, 2008
Dos and Don'ts of SOA Data Access    November 20, 2008
Mandrake Linux Founder Back, Virtually    November 20, 2008
Amazon: We're a Technology Company    November 19, 2008
Free Tech Newsletter -

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

Go to page: Prev  1  2  3  

Unified Destructors

Good programming practice dictates that you properly clean up all resources once you're done with them. This may involve closing a database connection, writing information to a log file, or anything else you deem appropriate. Prior to PHP 5, you were forced to devise your own methodology for dealing with such matters once work with an object is complete, resulting in inconsistent coding practice. PHP 5 resolves this with the introduction of unified destructors.

Like the new constructor syntax, destructors are called like so: __destruct(). Consider this example:

<?php
   class blog {

      private $title;

      function __construct() {
         $this->title = Just another rant...;
      }

      function __destruct() {
         $fh = fopen(bloglog.txt, a+);
         $date = date("Y-m-d at h:i:s");
         fwrite($fh, "New blog entry: $this->title. Created on $date\n");
         fclose($fh);
      }
   }

   $blogobj = new blog();
?>

Executing this script will result in a new object of type blog being created, and its $title property assigned the value "Just another rant...". Next, a handle to the file "bloglog.txt" will be opened, and an entry similar to the following will be added to it:

New blog entry: Just another rant.... Created on 2004-01-20.

Once appended, the filehandle is closed.

Class Constants

It's often useful to define constants on a per-class basis. This is now possible in PHP 5. For example, suppose that you wanted to define a standard thumbnail height and width for an image gallery class:

<?php

   class imagegallery {

      const THUMB_HEIGHT = 120;
      const THUMB_WIDTH = 120;

      private $height;
      private $width;

      function create_thumbnail($height="",$width="") {
         if (!isset($height)) $this->height = THUMB_HEIGHT;
         if (!isset($width)) $this->width = THUMB_WIDTH;
      }
   }

   echo "Default thumbnail height: ".imagegallery::THUMB_HEIGHT;
?>

These constants are only relevant to the imagegallery class, allowing you to reuse the terms "HEIGHT" and "WIDTH" within other classes or scripts comprising the application. Furthermore, as I demonstrate at the conclusion of the above example, you can reference the class constants from outside of the class by prefacing their name with the class name (not a class instance!) and double-colons.

Conclusion

PHP 5 offers major enhancements to its object oriented feature set, which certainly only adds to its allure as a powerful Web scripting language. However, these improvements are only the tip of the iceberg in regards to what PHP 5 has to offer. In the next installment of this two-article series, I'll round out the many features that users can expect from the forthcoming release.

I welcome questions and comments! E-mail me at jason@wjgilmore.com. I'd also like to hear more about your experiences experimenting with PHP 5!

About the Author

W. Jason Gilmore (http://www.wjgilmore.com/ ) is an Internet application developer for the Fisher College of Business. He's the author of the upcoming book, PHP 5 and MySQL: Novice to Pro, due out by Apress in 2004. His work has been featured within many of the computing industry's leading publications, including Linux Magazine, O'Reillynet, Devshed, Zend.com, and Webreview. Jason is also the author of A Programmer's Introduction to PHP 4.0 (453pp., Apress). Along with colleague Jon Shoberg, he's co-author of "Out in the Open," a monthly column published within Linux magazine.

Go to page: Prev  1  2  3  

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.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