#native_company# #native_desc#

Three Advanced Object-Oriented PHP Features You Need to Know Page 2

By W. Jason Gilmore
on June 15, 2010

The Standard PHP Library

Introduced in PHP 5.0, the Standard PHP Library (SPL) extension provides a series of object-oriented interfaces to common programming tasks. One of the most interesting features is the ArrayAccess interface, which you can use to manipulate objects using PHP’s native array syntax. For instance, a typical PHP class implementation will involve using a number of mutator and accessor methods to manipulate and retrieve object data, respectively:

$account = new Account(); $name = $account->findUsernameById(45); 

You can streamline this syntax by implementing the ArrayAccess interface, as demonstrated here:
<?php class Account implements ArrayAccess { function offsetExists($id) { return true; } function offsetGet($id) { return "Jason"; } function offsetSet($name, $id) { return true; } function offsetUnset($name) { return "Account deleted"; } } ?>
Of course, in a real-world situation you’ll likely implement each of the four interface methods offsetExists(), offsetGet(), offsetSet(), and offsetUnset()) by interacting with a database containing the account records. Either way, once implemented, you’ll be able to treat your class as an array, as demonstrated here:

<php $account = new Account(); if (isset($account[45])) { echo $account[45]; } ?> 

The SPL offers quite a few other interfaces which allow you to endow your classes with a variety of useful behaviors. See the PHP manual for all of the details.
In addition to these convenient object-oriented language enhancements, SPL significantly expands PHP’s datatype support by implementing advanced data structures such as heaps, queues, and stacks.

Late Static Binding

New to PHP 5.3, late static binding is a feature which can prove quite useful when working with class hierarchies, because it significantly streamlines the amount of code one had to write when using earlier versions of PHP in order to determine where exactly in the class hierarchy a static method call originated. Consider the following example, which loosely resembles a basic Active Record implementation. What do you think $table will be assigned following the call to Account::getTableName()?
class Record { protected static $tableName = "base"; public static function getTableName() { return self::$tableName; } } class Account extends Record { protected static $tableName = "accounts"; } $table = Account::getTableName();
In versions of PHP earlier than 5.3, $table would be assigned base because the self keyword was unable to determine that a child class originated the call to getTableName()! As of 5.3, this major inconvenience has been resolved, allowing PHP to bind to the originating caller at runtime, thereby setting $table to accounts.


I always recommend that newcomers to PHP take the time early on to get acquainted with the language’s object-oriented features, as it will inevitably lead to more maintainable, extensible code. However, only by diving into the more esoteric details of PHP’s object-oriented implementation will you really be able to stretch the language to its furthest capabilities. What other advanced PHP features interest you? Tell us about them in the comments!

About the Author

Jason Gilmore is the founder of EasyPHPWebsites.com and the author of
several popular books, including “Easy PHP Web sites with the Zend Framework” and “Beginning PHP and MySQL: From Novice to Professional” (currently in its third edition).
Check out his new DZone reference card, titled “Getting Started with the Zend Framework.”