November 26, 2014
Hot Topics:

The Object Oriented Improvements of PHP 5

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

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.



Page 3 of 3



Comment and Contribute

 


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

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel