PHP 5.6: What's New

PHP 5.6: What's New

It’s been a long time coming, but we finally have a new version of PHP. With it comes a some nice, new features, improvements to existing features, as well as features that have been removed or marked as deprecated.

Let’s dive in and take a look at everything that’s offered by the latest version.

Backward Incompatible Changes

In this part, I’ll be listing backwards incompatible changes; however, most of your PHP5 code will work in PHP 5.6 without any modifications.

json_decode

As per the JSON specification <code class="inline">json_decode() will immediately eliminate all non-lowercase variants of the JSON literals like true, false and null along with this it will set <code class="inline">json_last_error() accordingly.

GMP Resources

If you are not sure what GMP in PHP then I would advise looking at here. In PHP 5.6, GMP resources are objects. You do not need to make any changes in your existing code unless you are checking resource using explicitly using <code class="inline">is_resource().

Mcrypt

All Mcrypt function which expect key and IVs will not accept keys or IVs with incorrect size, these functions includes <code class="inline">mcrypt_encrypt(), <code class="inline">mcrypt_decrypt(), <code class="inline">mcrypt_cbc(), <code class="inline">mcrypt_cfb(), <code class="inline">mcrypt_ecb(), <code class="inline">mcrypt_generic() and <code class="inline">mcrypt_ofb().

Array Values and Overwriting

Before PHP 5.6 when you declare array in class property with explicit and implicit key, array value was overwritten silently when explicit key was the same as a sequential implicit key.

 class helloWorld {  
 const ONE = 1;

public $array = [  
 self::ONE => ‘Eclipse’,  
 ‘Sublime’,  
 ‘PHPStrome’,  
 ];  
 }

$hello = new helloWorld();

print_r($hello);

// Before PHP 5.6  
 array(‘Sublime’, ‘PHPStrome’)

// PHP 5.6  
 array(‘Eclipse’, ‘Sublime’, ‘PHPStrome’)  

New Features in PHP 5.6

Constant Scalar Expressions

With the release of PHP 5.6, it is possible to provide a scalar expression which includes both numeric and string literals. In previous versions of PHP, it was expected to be static value of constant, function arguments and property declaration.

 const ONE = 1;  
 // Scalar Expression in constant  
 const TWO = ONE * 2;

class helloWorld {  
 // Scalar Expression in Property  
 const THREE = TWO + 1;

 // Scalar Expression in Methods  
 public hello f($a = ONE + self::THREE) {  
 return $a;  
 }  
 }

echo (new helloWorld)->hello()."\n";  

Variadic Functions via “…”

Earlier we were using <code class="inline">func_get_args() to get all arguments available in function call, but with PHP 5.6, this can be removed as we can easily get that facility with <code class="inline">... operator.

 function myTools($name, …$tools) {  
 echo "Name:". $name.'<br />’;  
 echo "My Tool Count:". count(tools);  
 }

myTools(‘Avinash’, ‘Eclipse’);  
 // Output:  
 // Name: Avinash  
 // My Tool Count: 1

myTools(‘Avinash’, ‘Eclipse’, ‘Sublime’);  
 // Output:  
 // Name: Avinash  
 // My Tool Count: 2

myTools(‘Avinash’, ‘Eclipse’, ‘Sublime’, ‘PHPStrom’);  
 // Output:  
 // Name: Avinash  
 // My Tool Count: 3<b>  
 </b>  

Argument Unpacking

We can use the same operator <code class="inline">(...) to unpack any argument which is either an array or a set of traversable objects.

 function myTools($name, $tool1, $tool2, $tool3) {  
 echo "Name:". $name.'<br />’;  
 echo "Tool1:", $tool1.'<br />’;  
 echo "Tool2:", $tool2.'<br />’;  
 echo "Tool3:", $tool3;  
 }

 $myTools = [‘Eclipse’, ‘Subline’, ‘PHPStrom’];  
 myTools(‘Avinash’, …$myTools);  
 // Output:  
 // Name: Avinash  
 // Tool1: Eclipse  
 // Tool1: Subline  
 // Tool1: PHPStrome  

** Shorthand

** operator has been added for exponentiation, we have got support for the shorthand operator as easily.

 echo 2 ** 3;  
 echo "<br/>";  
 $a=2;  
 $a **= 3;  
 echo $a;

// Output  
 // 8  
 // 8  

Note that the order of operations comes into play using this operator.
Please take a look at the following example for a clear understanding:

echo 2 ** 2 ** 4;  

You might expect it to return 256 as grouping would be like <code class="inline">(2 ** 2) ** 4 but that is not the case here.
Real result would be 65536 as grouping would be from right to left and it will parse as <code class="inline">2 ** (2 ** 4).

phpdbg

An interactive debugger called phpdbg has been added in PHP 5.6. Please visit the official document for phpdbg.

This phpdbg debugger is implemented as SAPI module.

__debugInfo()

A new magic method added in PHP 5.6, this method allow to change properties and values of an object when the object is output using <code class="inline">var_dump().

  
 class demoDebugInfo() {  
 private $val;

 public function __construct($val) {  
 $this->val = $val;  
 }

 public function __debuginfo() {  
 return [  
 ‘iamchanged’ => $this->val + 3;  
 ];  
 }  
 }

$debugInfo = new demoDebugInfo(50);

// We will get altered array which is returned by __debuginfo megic method.  
 var_dump($debugInfo);  
   

Default Character Encoding

Default character set for <code class="inline">htmlentities(), <code class="inline">html_entity_decode() and <code class="inline">htmlspecialchars() functions can be set using  default_charset()

Large File Upload

It is possible to upload file larger than 2GB.

php://input is reusable

php://input can be used as many times you want to read data. This feature has great reduction in memory compare to reading <code class="inline">POST data.

use function and use const

<code class="inline">use operator has been offered to support extending of constants and functions, this can be performed by using the <code class="inline">const and the <code class="inline">use functions, respectively. Earlier this operator was limited to class only.

Deprecated Features

Call From Incompatible Context

Immediately it will generate <code class="inline">E_DEPRECATED error when you attempt to access non static method statically or static method using object context.

 class helloWorld() {  
 public static hiWorld () {

 }  
 }

$hello = new helloWorld();

$hello->hiWorld();

// Before PHP 5.6  
 E_STRICT error

// From PHP 5.6  
 E_DEPRECATED error<b>  
 </b>  

$HTTP_RAW_POST_DATA

<code class="inline">$HTTP_RAW_POST_DATA is deprecated now, we should be using <code class="inline">php://input instead.`

Encoding Setting

With the launch of <code class="inline">default_charset() configuration related option is deprecated for <code class="inline">iconv and <code class="inline">mbstring.

List of Changed Functions

List of all changed functions in PHP 5.6 can be found here.

List of New Functions

List of all New functions in PHP 5.6 can be found here.

Conclusion

While the PHP team is working on PHP6, PHP7 and PHPNG, I would say PHP 5.6 has covered a shipped with a solid amount of improvements and feature additions.

Subscribe

Stay up to date! Get all the latest & greatest posts delivered straight to your inbox

You've successfully subscribed to Salehin Khan
Great! Next, complete checkout for full access to Salehin Khan
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info is updated.
Billing info update failed.