December 19, 2014
Hot Topics:

Elevate Your PHP Development with PHP 5.3's Powerful New Features

  • March 31, 2010
  • By Jason Gilmore
  • Send Email »
  • More Articles »

By typical development conventions, PHP 5.3 only qualifies as a "point release." However, the features packed into this new version are easily the most significant PHP development enhancements since PHP 5.0 was released in 2004. The powerful new PHP 5.3 features include namespace support, lambda functions and closures, late static binding, improved Windows support, and a host of syntactical additions. This article provides all the information you need to begin taking advantage of these PHP 5.3 goodies.

Namespace Support

The addition of namespaces is considered the most significant addition of the PHP 5.3 release. Namespaces provide developers with an effective means to organize code in a way that greatly diminishes the possibility of naming collisions (notably classes, functions, and constants) when integrating third-party code. Put into practical terms, you can now safely create a library capable of interacting with Amazon's A2S web service and namespace it using a unique moniker such as A2S. However, because namespace hierarchies are also supported, you probably should use a convention that guarantees uniqueness, such as Com\Wjgilmore\A2s.

Associating a file (say, a2s.php) to a namespace is easy; just use the namespace keyword like this:

<?php

  namespace Com\Wjgilmore\A2s;

  class AmazonAssociatesWebService
  {

    private $accessKeyID = ''
  
    function __construct($accessKeyID) {
      $this->accessKeyID = $accessKeyID;
    }

    function authenticate() {
      ...
    }

  }

?>


To use a namespaced class within your code, you first integrate the code using the usual require_once() statement, and then call the class by prefixing the class name with the namespace prefix, like this:

<?php
  require('a2s.php');
  $a2s = new Com\Wjgilmore\A2s\AmazonAssociatesWebService('secretkey');
?>


Because referencing lengthy namespace designations within your code will quickly become tedious, PHP 5.3 allows you to create an alias that can subsequently be used in place of the moniker, like this:

<?php
  require('a2s.php');
  
  use Com\Wjgilmore\A2s as A2s;

  $a2s = new A2s\AmazonAssociatesWebService('secretkey');
?>


Namespaces will no doubt play a major role in many aspects of the PHP language, including solutions such as the Zend Framework, which have relied upon necessarily long naming conventions (such as Zend_Db_Table_Row_Abstract) for classes.

Closures

Anonymous functions, also known as closures, play a significant role in languages such as JavaScript and Ruby. PHP 5.3 makes this powerful feature available to PHP developers, allowing you to write more succinct code. For instance, many PHP functions accept a function name as a variable, the latter known as a callback function. This callback function will execute and return its output to the caller. The caller can then use the output to perform other tasks. For instance, PHP's preg_replace_callback() function is handy for using a regular expression to search a string and replace any occurrences that have been transformed by a callback function with modified versions of the occurrences.

Here's how you would usepreg_replace_callback() to convert a string containing U.S.-localized dates to a more suitable European format prior to version 5.3:

$history = 'The Magna Carta was signed on 06-15-1215.';

echo preg_replace_callback("|(d{2})-(d{2})-(d{2})|", 'convertUsToEU', $history);

function convertUsToEU($matches) {
  return "{$matches[2]}-{$matches[1]}-{$matches[3]}";
}


This seems like quite a bit of formality just to swap a date around, right? Using closures, you can move the function definition into the preg_replace_callback() call, like this:

$history = 'The Magna Carta was signed on 06-15-1215.';

echo preg_replace_callback("|(d{2})-(d{2})-(d{2})|", 
                           function($matches) {   
                             return "{$matches[2]}-{$matches[1]}-{$matches[3]}"; 
                           },
                           $history
     );






Page 1 of 2



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