December 21, 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 »

Late Static Binding

The object-oriented development approach provides an easy and intuitive way to create hierarchical data structures, and to overload the properties of a parent class to fit the specific characteristics of its children. Consider the following example:

class Automobile {
  static $horn = "beep";
  static function honkHorn() {
    echo self::$horn;
  }
}

class GeneralLee extends Automobile {
  static $horn = "HONKITY HONK HONKY TONK!!!";
}


What do you believe will happen when the following command is called?

GeneralLee::honkHorn();


It seems logical that HONKITY HONK HONKY TONK!!! will be displayed, but in fact the meek beep will be returned instead. This is because PHP sees the $horn attribute's self prefix and presumes you've intended to output the attribute located within that particular class. To fix this issue, you can take advantage of PHP 5.3's new late-static binding feature: use the static keyword to reference the class attribute that will be displayed rather than the self keyword:

class Automobile {
  static $horn = "beep";
  static function honkHorn() {
    echo static::$horn;
  }
}

class GeneralLee extends Automobile {
  static $horn = "HONKITY HONK HONKY TONK!!!";
}


Upgraded MySQL Support

PHP's relationship with MySQL spans more than a decade, and the individual success of each technology is undoubtedly in part attributed to the respective communities' longstanding support for one another. As such, a great deal of attention is paid to PHP and MySQL performance issues. The latest proof of these efforts is present in PHP 5.3, with the introduction of a MySQL native driver for PHP, known as mysqlnd.

The mysqlnd driver doesn't change the way you interact with MySQL within your PHP applications; rather it improves performance thanks to its close integration with PHP and optimized approach to communicating with MySQL. This new driver also resolves licensing compatibility issues, which were introduced when MySQL changed its licensing guidelines a few years back. As a bonus, when you use this driver, you can to interact with a remote MySQL server without having to first install a MySQL client on the local server.

Ternary Shortcut

One of my favorite PHP syntax features is the ternary operator, which can significantly reduce the amount of code needed to carry out simple conditional tasks. Consider the following if statement:

if (isset($_POST['log_out_when_browser_closes'])) {
  $logUserOut = 1;
} else {
  $logUserOut = 0;
}


Using the ternary operator, you can reduce this statement to a single concise line:

$logUserOut = isset($_POST['log_out_when_browser_closes']) ?: 0;


Upgrading to PHP 5.3

If you haven't upgraded yet, what are you waiting for? Packages are available for all major platforms, including Windows, Mac OS X, and various Linux distributions.

Unfortunately, at the time of this writing Ubuntu did not offer PHP 5.3 as part of the official distribution, although several tutorials show you how to install it regardless. For instance, check out this tutorial and the accompanying comments for more information.

About the Author

Jason Gilmore is founder of EasyPHPWebsites.com. He is the author of several popular books, including "Easy PHP Websites with the Zend Framework," "Easy PayPal with PHP," and "Beginning PHP and MySQL, Third Edition."





Page 2 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