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"
Oh, great, another way for us to break our web applications. Boy, I can\’t wait to be first in line to have that happen.
It\’s about time. PHP has suffered from some of the cruft hanging around in its codebase for a long time. If people finally have to fix their crappy apps that were still using register_globals, then PHP 6 is worth the upgrade pains.
Backwards compatibility is great, but when old code is written in an inherently insecure way, the only solution is to break from the past, and suffer the short term pain. The long term gain is worth it.
PHP 6 may be a big upgrade, but it\’s not that huge of a shift in terms of language design. Python 3 was a bigger change, and don\’t even mention Perl 6 which is a completely new language (one which I support by the way.)
So stop moaning how PHP 6 is going to break your applications. If it does, then they weren\’t well written to begin with. Bring on the new!
+1 for getting rid of some of the more egregious legacy cruft from the PHP3/4 days.
-1 for the absolutely asinine namespace implementation; several other languages (Python, C#, Eiffel) do the equivalent much better.
-1 for making Mac OS X a third-class citizen (behind Linux and Windows); updates/upgrades are a hundred times more difficult/painful/risky than on any other platform.
Having nearly finished (yet another) book on PHP-based Web development, maybe I should go pick up mod_python again. I don\’t drink or do drugs, so Rails and mod_perl aren\’t things I\’d even poke at.
How does PHP6 make Mac OS X a third-class citizen? I don’t get it.
PHP 5.3 and PHP 6 drop the ZE1 compatibility mode. The PHP 4 code shown here works without even an E_STRICT complain in PHP 5, and should still work the same in PHP 6.
ZE1 compatability mode was off by default in php 5, so if your objects worked in php 5 they should still work in php 6.
See also my blog at: http://darrendev.blogspot.com/2009/08/php6-maybe-not-so-painful.html
Thak you for nice article on PHP6…..Its explain main feature of PHP6 which is understandable for beginner or any technical person.
Hello! I simply want to offer you a big thumbs up for the great info you’ve got right here on this post. I am coming back to your site for more soon.
Wonderful blog! I found it while searching on Yahoo News.
Do you have any suggestions on how to get listed in Yahoo News?
I’ve been trying for a while but I never seem to get there! Thank you
I’ve been surfing online more than three hours today, yet I never found any interesting article like yours. It’s pretty worth enough for me.
In my opinion, if all website owners and bloggers made good content as you
did, the web will be a lot more useful than ever before.
I simply couldn’t leave your site prior to suggesting that I really loved the usual information an individual supply on your guests? Is gonna be back frequently in order to check out new posts
My weblog; exteen.com
Simply wish to say your article is as surprising. The clarity in your post is just great
and that i could think you’re an expert on this subject. Fine together with your permission allow me to snatch your RSS feed to stay updated with impending post. Thank you one million and please continue the gratifying work.
My family every time say that I am killing my time here at
net, but I know I am getting experience all the time by reading such pleasant
content.
Thank you for the good writeup. It in fact was a entertainment account
it. Look advanced to far delivered agreeable from you!
However, how can we communicate?
I got this site from my pal who informed me concerning this site and now this time I am browsing this web site and
reading very informative articles or reviews
at this time.
I have read so many posts concerning the blogger lovers except this piece
of writing is actually a fastidious piece of writing, keep it up.
Now I am going to do my breakfast, later than having my breakfast coming over again to read additional news.
whoah this blog is great i really like reading your articles.
Keep up the great work! You already know, a lot of people are looking round for
this info, you can aid them greatly.