dcsimg

Get Ready for PHP 6

PHP 6, the next major revision of the popular Web application development language, looms on the horizon and promises many changes. Learn what's new and what's obsolete and how to prepare your code for tomorrow.

PHP offers a default encoding via the unicode.fallback_encoding in case you don’t specify one of the encodings in the keys above. If you don’t specify the unicode.fallback_encoding, PHP 6 defaults to UTF-8.

unicode.fallback_encoding = "utf-8"

When the unicode.semantics setting is turned On, PHP treats all of your string literals, variables received from HTTP requests, and PHP identifiers as Unicode. Binary strings must be declared explicitly so that they can be handled correctly:

$mystr = (binary)$unicodedStr;

INI files are assumed to be UTF-8 encoded unless unicode.semantics is set to Off. If Unicode characters are specified in an INI file and Unicode support has been disabled, the value will be used as-is (like PHP 5).

The output encoding specified by unicode.output_encoding includes the output of methods like echo and print that write to standard output. Setting the value doesn’t make PHP convert existing strings to the specified output encoding. Binary strings should be converted using the appropriate conversion functions (see below).

Script encoding set through unicode.script_encoding allows you to use Unicode characters inside your PHP scripts. There is also an override that can be used inside a single script in case you have exceptions to the INI value. For example, the following declaration, which must be the first line in the script, allows you to use specify ISO-8859-1 encoding for a single script:

<?php declare(encoding='utf-8') ?>

Unicode characters can be specified inside strings with new escape sequences, \uXXXX and \UWWWWWW, where XXXX and WWWWWW are hexidecimal values of the Unicode characters. The new escape sequences can be specified in both single- and double-quoted strings.

PHP functions that work with strings are being converted to support Unicode. You can check on the progress of this conversion at http://www.php.net/~scoates/unicode/render_func_data.php. The extensions that have been fully converted are listed in the README.UNICODE file distributed with the PHP 6 source package.

The unicode.filename_encoding is used to specify the character encoding for file and directory names. This setting should be the same as your filesystem’s character encoding, so do some research to make sure you’re specifying the correct value. It won’t do you any good to specify unicode encoding for your file and directory names if your filesystem doesn’t support it.

If you’re working with text inside files (as opposed to working with files in binary streams) using the FILE_TEXT parameter to file_*_contents() or t to fopen(), you should be aware of the file’s encoding. By default, PHP 6 uses UTF-8 when reading from and writing to a file when in text mode. The stream_context_create() function can be used to convert the data in files to different encodings.

PHP 6 enables you to convert strings using the functions below to cast the strings:

Function Name Purpose
(binary) Casts string to binary type
(unicode) Casts string to a unicode string
(string) Casts string to a unicode string as long as unicode.semantics is on,
otherwise casts it to a binary string.

More details about Unicode support in PHP 6 can be found in the README.UNICODE file that is included with the PHP 6 source package.

Object-Oriented Features

In addition to function changes, PHP 6 will not support compatibility for the PHP 4 object model. If you have objects in your PHP code, make sure they’re PHP 5 objects. The PHP 4 object model compatibility was only available after 5.0.0 and was removed in PHP 5.3.0, so any object that works in 5.3 later versions will work fine in PHP 6.

Here is an example of a PHP 4 class:

<?php
class MyClass {
  var $myVar;

  function foo()
  {
    echo "bar";
  }
}
?>
  

And an example of a PHP 5 class:

<?php
class MyClass {
  private $myVar;

  public function foo()
  {
    echo "bar";
  }
}
?>

The ability for PHP 5 to support the PHP 4 model was to make it easier to migrate. However, the compatibility was never completely functional. Removing compatibility wasn’t originally planned for release before PHP 6, but ended up being included in PHP 5.3.0.

If you are migrating from PHP pre-5.3.0, make sure to update your objects to the new PHP 5 object model.

E_ALL and E_STRICT

Prior to PHP 6, E_ALL might not really include all the errors. As of PHP 5.3.0, E_ALL still does not include E_STRICT. E_STRICT was new as of PHP 5 and is helpful because it alerts you of using deprecated functions and violating coding standards. To show these notifications, E_STRICT must be explicitly called out in the INI file.

Prior to PHP 6: error_reporting = E_ALL | E_STRICT

The same results in PHP 6:error_reporting = E_ALL

An example of a coding standard violation is calling a non-static method on a class as if was static. For example:

<?php
class MyClass {
  private $myVar;

  public function foo()
  {
    echo "bar";
  }
}

$my = new MyClass();
$my::foo();
?>

When this script is called, it throws the error:

Strict Standards: Non-static method MyClass::foo() should not be called statically
in public_html/test_class.php on line 13 bar

FreeType1 and GD1 support

It should come as no surprise that FreeType 1.x and GD 1.x are no longer supported. Their replacements–FreeType 2.x and GD 2.x–are. You can compile PHP with the --with-freetype-dir and --with-gd options to enable them.

Chances are you aren’t using the older versions of FreeType or GD, but if you’re doing image manipulation review your code carefully to make sure.

Summary

Overall, PHP 6 offers many improvements. In addition to Unicode support, removing some deprecated features allows developers to write tighter PHP code. Downloading the PHP 6 sources now and building them out to a development environment is a good way to start testing your PHP code now to catch any problems early.

Comments on "Get Ready for PHP 6"

Great post. Just a heads up – I am running Ubuntu with the beta of Firefox and the navigation of your blog is kind of broken for me.

Leave a Reply