What's new in PHP V5.3, Part 5: Upgrading from PHP V5.2

Changes, compatibility, and deprecation

This "What's new in PHP V5.3" series covers new and exciting features in PHP V5.3, which is scheduled to release soon. Part 1 looks at the changes made to the object-oriented programming and object handling in PHP V5.3, Part 2 looks at lambda functions and closures. In Part 3, we look at namespaces, which is one of the most anticipated and the most debated feature in this release of PHP. In Part 4, we take a close look at Phar, which is an archive format that can be used within PHP. In this final part of the series, learn about things to consider when upgrading from PHP V5.2. There are changes that break backward-compatibility and features that are deprecated in PHP V5.3 for removal in future versions. Some enhancements to existing features within PHP are also covered.

Share:

John Mertic, Software Engineer, SugarCRM

John Mertic graduated with a degree in computer science from Kent State University and is currently a software engineer at SugarCRM. He has contributed to many open source projects, most notably PHP projects; he is the creator and maintainer of the PHP Windows Installer.



24 February 2009

Also available in Russian Japanese Vietnamese

Introduction

This series has concentrated on new features in PHP V5.3, such as namespaces, closures, object handling, object-oriented programming, and Phar. While these flashy new features are a welcome addition to the language, PHP V5.3 was also designed to further streamline PHP. It builds upon the popular and stable PHP V5.2 and enhances the language to make it more powerful. In this article, learn about changes and considerations when upgrading from PHP V5.2.


Syntax changes

Additions to the language, with namespaces and closures (discussed in Part 2 and Part 3), have added more reserved words. Starting in PHP V5.3, namespace can no longer be used as an identifier. The closure class is now a reserved class, but it is still a valid identifier. Listing 1 shows examples of statements that no longer work in PHP V5.3 because of the additional reserved words.

Listing 1. Invalid PHP statements
// the function definition below will throw a fatal error in PHP 5.3, but is perfectly 
// valid in 5.2
function namespace() 
{
....
}

// same with this class definition
class Closure
{
....
}

Support for the goto statement was also added in PHP V5.3. Now goto is a reserved word. goto statements are not common in modern languages (you might remember using them in BASIC), but there are occasionally use cases where they are handy. Listing 2 has an example of how they work.

Listing 2. goto statements in PHP
echo "This text will get outputted";
goto a;

echo "This text will get skipped";

a:
echo "This text will get outputted";

One possible use case for gotos is for breaking out of deeply nested loops and if statements. This will make the code much clearer to read.


Changes to functions and methods

Though there are no major changes to functions and methods in PHP V5.3, there are a few enhancements to help with outstanding issues in PHP and to improve performance. This section discusses a few of the more notable changes.

In previous versions of PHP, the array functions atsort, natcasesort, usort, uasort, uksort, array_flip, and array_unique let you pass objects instead of arrays as parameters. The functions then treat the properties of the objects as the array keys and values. This is no longer available in PHP V5.3, so you need to cast the objects to arrays first. Listing 3 shows how to change your code.

Listing 3. Changing code to cast objects to arrays for certain functions
$obj = new stdClass;
$obj->a = '1';
$obj->b = '2';
$obj->c = '3';

print_r(array_flip($obj)); // will NOT work in PHP 5.3, but will in PHP 5.2

print_r(array_flip((array) $obj)); // will work in PHP 5.3 and 5.2

The magic class methods are now much more strictly enforced. The following methods must have public visibility:

  • __get
  • __set
  • __isset
  • __unset
  • __call

You can use the new __callStatic() magic method in cases where __call was used in a static context as a workaround for this change. The required arguments for these methods are enforced and must be present with the exception of the __isString() magic method, which accepts no parameters. Listing 4 shows how to use these methods and the required parameters for them.

Listing 4. Using the magic methods
class Foo
{
    public function __get($key) {} // must be public and have one parameter
    public function __set($key,$val) {} // must be public and have two parameters

    public function __toString() {} must be public and have no parameters
}

Several functions that previously were not supported on PHP with Windows are now supported in PHP V5.3. For example, the getopt() function is designed to parse the options for calling a PHP script from the command line. inet_ntop() and inet_pton(), the functions for encoding and decoding Internet addresses, now work under Windows® as well. There are several math functions, such as asinh(), acosh(), atanh(), log1p(), and expm1(), which now have Windows support.


Extension changes

The PHP Extension C Library (PECL), has been the breeding ground for new extensions in PHP. Once an extension is mature and stable and is viewed as a useful function for part of the core distribution, it is often added during major version changes. In this spirit, starting in PHP V5.3, the following extensions are part of the core PHP distribution.

FileInfo
Provides functions that help detect the content type and encoding of a file by looking at certain magic byte character sequences in the file.
intl
A wrapper for the International Components for Unicode (ICU) library, providing functions for unicode and globalization support.
Phar
A PHP archiving tool discussed in Part 4.
mysqlnd
A native PHP driver for MySQL database access that's a replacement for the earlier MySQL and MySQLi extension which leveraged the libmysql library.
SQLite3
A library for using SQLite V3 databases.

When an extension is no longer actively maintained, or is deemed unworthy of distribution with the core PHP distribution, it is often moved to PECL. As part of the shuffling in PHP V5.3, the following extensions have been removed from the core PHP distribution and are maintained as part of PECL.

ncurses
An emulation of curses, which is used to display graphical output on the command line.
fpdf
Handles building and using forms and form data within PDF documents.
dbase
Provides support for reading and writing dbase compatible files.
fbsql
Supports database access for Frontbase database servers.
ming
An open source library that allows you to create Flash 4 animations.

The Sybase extension has been removed entirely and is superseded by the sybase_ct extension. The sybase_ct extension is fully compatible with the former and should be a drop-in replacement. The newer function will use the Sybase client libraries you need to install on your Web server.


Build changes

With the strong focus on refining the build process in PHP V5.3, it's easier to build PHP on all platforms. To maintain consistency between PHP builds and to provide a guaranteed set of components in PHP, the PCRE, Reflection, and SPL extensions can no longer be disabled in the build. You can build distributable PHP applications that use these extensions and are guaranteed that they will be available for use.

A new team took over the PHP Windows build in the last year. Starting in PHP V5.3, the team will provide several improvements for users on Windows. The new builds will target the 586 architecture (Intel® Pentium® or later) and will require Windows 2000/XP or later, removing support for Windows 98/NT and earlier. PHP builds built with Microsoft® Visual Studio® 2008 and builds targeting the x86-64 architecture will be built. They offer improved performance when working with FastCGI on the Microsoft IIS Web server or with Apache built with the same compiler and architecture. The Windows installer is also being improved to better configure PHP with the Microsoft IIS Web server. The team launched a Web site specific to PHP on Windows (see Resources).


.ini changes

An important feature of PHP is that its behavior can be configured using an .ini file. In PHP V5.3, several problematic directives for this file have been removed, such as the zend.ze1_compatibility_mode setting. You now have tremendously improved flexibility when using this file.

There are two major improvements to the php.ini file:

  • You can have variables within the php.ini file. This is very handy for removing redundancies within the file, and it's easier to update the file if changes are needed. Listing 5 shows an example.
    Listing 5. Variables in php.ini file
    foo = bar 
    
    [section] 
    newfoo = ${foo}
    foo and newfoo have the same value.
  • You can make per-directory and per-site PHP ini settings, similar to making those same settings with the Apache configuration files. The advantage here is that the syntax becomes consistent across all of the various SAPIs PHP can run under. Listing 6 shows how this works.
    Listing 6. Per-site and per-directory .ini settings
    [PATH=/var/www/site1] 
    
    ; directives here only apply to PHP files in the /var/www/site1 directory 
    
    [HOST=www.example.com] 
    
    ; directives here only apply to PHP files requested from the www.example.com site.

You can also have these .ini directives created in user-specified .ini files, located in the file system itself, in the same way that .htaccess files work under the Apache HTTP Web server. The default name for this file is specified by the user_ini.filename directive. The feature can be disabled by setting this directive to an empty value. Any per-site and per-directory directives cannot be overridden in a user-specified .ini file.


Deprecated items

PHP V5.3 starts officially deprecating older functions that will no longer be available in future versions of PHP. When you use these functions, an E_DEPRECATED error will be emitted. The following functions are deprecated for PHP V5.3:

  • Ticks (declare(ticks=N) and register_tick_function() ), which were designed to have a function call for every n statements executed by the parser within the declare() block. They're being removed because of numerous breaks in their function and because the feature isn't used very often.
  • define_syslog_variables(), which initializes all syslog related variables. This function isn't required because the constants it defines are already defined globally. Simply removing this function call should be all that is necessary.
  • The ereg regular-expression functions. It's recommended that you use the PCRE regular-expression functions instead, since they are much faster and more consistent with regular expressions used in other languages and applications. Support for the ereg functions is being removed so PHP can standardize with one regular-expression engine.

It is recommended that you migrate away from these features with PHP V5.3. Future major PHP releases will drop support for the above items.


Summary

PHP V5.3 has numerous new features and has "cleaned up" several items. There are some backward-compatibility issues. This article provides some guidance for migrating your Web application to work with PHP V5.3. For the latest details regarding PHP V5.3 see the PHP wiki, which has notes on any other changes that might affect your applications.

Resources

Learn

  • Learn more about closures from Wikipedia.
  • PHP For Windows is dedicated to supporting PHP on Microsoft Windows. It also supports ports of PHP extensions or features, and provides special builds for the various Windows architectures.
  • Visit the PHP wiki to learn all about changes to PHP V5.3.
  • Read "A PHP V5 migration guide" to learn how to migrate code developed in PHP V4 to V5.
  • "Connecting PHP Applications to Apache Derby" shows you how to install and configure PHP on Windows (some steps are applicable to Linux®).
  • For a series of developerWorks tutorials on learning to program with PHP, see "Learning PHP, Part 1," Part 2, and Part 3.
  • Planet PHP is the PHP developer community news source.
  • The PHP Manual has information about PHP data objects and their capabilities.
  • Visit Safari Books Online for a wealth of resources for open source technologies.
  • PHP.net is the central resource for PHP developers.
  • Check out the "Recommended PHP reading list."
  • Browse all the PHP content on developerWorks.
  • Follow developerWorks on Twitter.
  • Expand your PHP skills by checking out IBM developerWorks' PHP project resources.
  • To listen to interesting interviews and discussions for software developers, check out developerWorks podcasts.
  • Using a database with PHP? Check out the Zend Core for IBM, a seamless, out-of-the-box, easy-to-install PHP development and production environment that supports IBM DB2 V9.
  • Stay current with developerWorks' Technical events and webcasts.
  • Check out upcoming conferences, trade shows, webcasts, and other Events around the world that are of interest to IBM open source developers.
  • Visit the developerWorks Open source zone for extensive how-to information, tools, and project updates to help you develop with open source technologies and use them with IBM's products.
  • Watch and learn about IBM and open source technologies and product functions with the no-cost developerWorks On demand demos.

Get products and technologies

  • PHPMyAdmin is a popular PHP application that has been packaged in Phar to use as an example of how easy using Phar archives are.
  • Get PHP V5.2.6.
  • Innovate your next open source development project with IBM trial software, available for download or on DVD.
  • Download IBM product evaluation versions, and get your hands on application development tools and middleware products from DB2®, Lotus®, Rational®, Tivoli®, and WebSphere®.

Discuss

Comments

developerWorks: Sign in

Required fields are indicated with an asterisk (*).


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Information in your profile (your name, country/region, and company name) is displayed to the public and will accompany any content you post, unless you opt to hide your company name. You may update your IBM account at any time.

All information submitted is secure.

Choose your display name



The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

Required fields are indicated with an asterisk (*).

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


All information submitted is secure.

Dig deeper into Open source on developerWorks


static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Open source
ArticleID=369869
ArticleTitle=What's new in PHP V5.3, Part 5: Upgrading from PHP V5.2
publish-date=02242009