's New in PHP 5.6
What's New in PHP 5.6
by John Coggeshall
Back in August the PHP project released PHP 5.6.0, a major step forward for PHP with tons of new language-level features, OCI8 improvements, and other improvements bolstering power and productivity in the language. I'll walk you through the major highlights in this article so you can begin taking advantage of all of that hard work in no time!
Language Features
PHP 5.6 is packed full of new language features designed to improve the readability and abilities of the language, from varadic function prototypes to function and constant importing. These features are designed to further tighten the experience in writing PHP applications as well as further polish previously released critical features (such as namespaces). Let's take a look at the big ones, and how they can be used in your applications. Most of these language features don't necessarily allow you to do something you could not do in previous versions of PHP, but rather allow you to do them in a much cleaner way from a syntax perspective making your code less cluttered and easier to maintain. For example, let's start with a simple one. In versions of PHP prior to 5.6, calculating the value of an exponentitial was done using the pow() function. While totally functional, as of PHP 5.6 you can now use the ** operator to do the same thing:
<?php $result = 2 ** 4; // 16 ?>
Don't worry, PHP 5.6 has a few features with a lot more meat than that simple enhancement, let's take a look at them:
Varadic Functions
PHP has always had the ability to create methods and functions that supported a variable number of parameters when called, but frankly the implementation of it has always been a pain point for a lot of developers because it relied on the use of functions such as func_get_args() in order to be done. Starting with PHP 5.6 however, a new syntax is available which will make such patterns much cleaner to implement. Consider the following code, which concat's a variable number of strings together written in the style of PHP 5.5 and below:
<?php function concat_str() { $retval = ""; foreach(func_get_args() as $arg) { $retval .= $arg; } return $retval; } ?>
While this approach has gotten the job done for PHP for as long as the language has been remotely mature, many found the approach cumbersome especially when compared to other modern languages. To address this, a new syntax and functionality has been introduced to allow the same functionality in a much more concise fashion as shown:
<?php function concat_str(...$strings) { $retval = ""; foreach($strings as $str) { $retval .= $str; } return $retval; } ?>
Note that this new syntax can be added to the end of any function's argument lists, resulting in any arguments not explictly defined to be captured as an array in the variable defined.
But, that's not all! Along with simplifying the approach to capturing variable argument lists in functions and methods, the same syntax can also be used to pass in arrays as function parameters. In previous versions of PHP, you would have to use something like call_user_func_array() to pass in function or method arguments dynamically to a function, but as of PHP 5.6 the following syntax can be used:
<?php function add_together($a, $b) { return $a + $b; } ?> $inputs = [5, 8]; $result = add_together(...$inputs); // 13
As you can see the new syntax is pretty useful, and certainly makes the readability of the prototypes for functions much clearer.
Constant Scalar Expressions
Another minor annoyance in PHP version before 5.6 was the inability to use any sort of expresions in constants defined using the const keyword. For those of you who have ever worked with something like bitmasks, the ability to use scalar expressions with constant values is very useful. As of 5.6, this is possible:
<?php class foo { const MY_CONST = 1 << 2; } ?>
Namespacing of Constants and Functions
While the introduction of namespaces into PHP was a major winfall for the language there were a few unfortunate syntaxical artifacts when dealing with procedural code and class constants that complicated things more than necessary. As of PHP 5.6, these issues have been addressed through the introduction of the use function and use const keywords. Through these, you can now import procedural functions or class constants into the namespace without having to reference the namespace they reside as shown below:
<?php namespace My\Namespace; use Another\Namespace\my_function as my_function; use Another\Namespace\MY_CONSTANT as MY_CONSTANT; $result = my_function(MY_CONSTANT); ?>
Like the other language features introduced in PHP 5.6, the namespacing improvements improve the readability of your PHP applications (although admittingly importing of a procedural function into other namespaces is not a very common practice).
OCI8 Improvements
Along with the language enhancements in PHP 5.6, a number of extensions including the OCI8 extension were also enhanced. Specifically, the OCI8 extension for PHP now supports binding the Oracle Database 12c PL/SQL BOOLEAN datatype as well as PL/SQL Implicit Result Sets. These allow results from multiple queries to be easily fetched from databsae procedures as shown below:
<?php $sql = â€˜DECLARE c1 SYS_REFCURSOR; BEGIN OPEN c1 FOR SELECT last_name FROM employees; DBMS_SQL.RETURN_RESULT(c1); OPEN c1 FOR SELECT city FROM locations; DBMS_SQL.RETURN_RESULT(c1); END;â€™; $s = oci_parse($c, $sql); oci_execute($s); while (($s_c = oci_get_implicit_resultset($s))) { oci_set_prefetch($s_c, $pf); while (($row = oci_fetch_array($s_c, OCI_ASSOC+OCI_RETURN_NULLS)) != false) { my_print_row($row); } } ?>
In addition to these enhancements, OCI8 also now supports the powerful Dtrace Dynamic Trace static probes. These incredibly useful, always-available hooks allow amazing insight into the workings of your code base and are supported on a number of platforms including Oracle Linux.
Other Enhancements
Complementing the language features introduced in PHP 5.6 a number of enhancements to the tools and extensions available in your garden-variety installation of PHP have been made which I will discuss now. If you don't necessarily have a use for some of these features it will be certainly be a less important reason to upgrade but of course it's good to know what they are.
File uploads beyond 2GB
PHP previously had a restriction on the maximum size of a single file when uploading it to a PHP script of 2 gigabytes. As of PHP 5.6 this restriction has been removed allowing for the upload of single files of any size you choose. Why you would ever attempt to upload a 2GB+ file is unclear to me, but if you are one of the few with a genuine use-case for it this enhancement is for you!
Reduced POST data memory footprint (and BC breaks)
When dealing with POST data PHP used to be pretty wasteful for the majority of use-cases by populating not only the $_POST superglobal but also the $HTTP_RAW_POST_DATA superglobal on each request. As a general rule, the majority of developers aren't interested in extracting the raw HTTP POST data during a request and the decision was made to remove this means of accessing the data in PHP 5.6.
Moving forward, raw HTTP POST data can still be retrieved, but doing so will require reading in from the php://stdin file stream during an HTTP POST request.
Improved ZIP file handling
If you work with compressed ZIP files in PHP you are most likely using the ZIP extension to do it. While a nice extension, prior to PHP 5.6 there were a number of rather glaring omissions that had a tendancy to frustrate developers trying to use it. One such omission was the inability for PHP to create compressed archives that were password protected, an issue resolved in PHP 5.6.
Built in debugging for PHP
One of the nicer features now available in stock PHP as of PHP 5.6 is the PHPDBG debugger, allowing you to either through a UI or from the console debug your PHP scripts in a fashion similar to what's been available in other languages or professional PHP IDEs for years. I personally expect this feature to be getting even more attention over the next few months while discussions around the remote debugging protocol and further enhancements to the abilities of the debugger continue to rage within the PHP internals mailing lists. Needless to say however, the introduction of native debugging facilities into PHP itself is a feature long awaited by developers everywhere!
Conclusion
As you can see, with PHP 5.6 the tireless developers of the PHP project continue to enhance and innovate around the technology in order to assure that PHP's dominance in the web development space continues. With the planning and development of the next major version of PHP well underway you can expect even more great things from this resilient language to come!