The purpose of this site is to build an active community for UIMinds, PHP, HTML, JavaScript, Jquery Developers and UI/UX Designers to Network, Exchange ideas and talk about Code, Architecture, Innovation, and open source in general. Share the Open Source, Examples, Inspirations and best practices.

Monday, October 27, 2014

PHP 5.6: What's New

It's been a long time coming, but we finally have a new version of PHP. It offers some nice new features and improvements, while other features have been removed or marked as deprecated.

Let's dive in and take a look at everything the latest version offers.

Backward Incompatible Changes

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

json_decode

As per the JSON specification, json_decode() will immediately eliminate all non‑lowercase variants of the JSON literals like true, false, and null. It will also setjson_last_error() accordingly.

GMP Resources

If you are not sure what GNU Multiple Precision (GMP) in PHP is, then I would advise you to look at this article on PHP.net. In PHP 5.6, GMP resources are objects. You do not need to make any changes in your existing code unless you are checking a resource explicitly using is_resource().

Mcrypt

All Mcrypt functions that expect keys and IVs will not accept keys or IVs with incorrect size. These functions include mcrypt_encrypt(), mcrypt_decrypt(), mcrypt_cbc(),mcrypt_cfb(), mcrypt_ecb(), mcrypt_generic(), and mcrypt_ofb().

Array Values and Overwriting

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

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

classhelloWorld {

constONE = 1;

public$array= [

self::ONE => 'Eclipse',

'Sublime',

'PHPStorm',

];

}

$hello= newhelloWorld();

print_r($hello);

// Before PHP 5.6

array('Sublime', 'PHPStorm')

// PHP 5.6

array('Eclipse', 'Sublime', 'PHPStorm')

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 a static value of constant function arguments and property declaration.

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

constONE = 1;

// Scalar Expression in constant

constTWO = ONE * 2;

classhelloWorld {

// Scalar Expression in Property

constTHREE = TWO + 1;

// Scalar Expression in Methods

publichello f($a= ONE + self::THREE) {

return$a;

}

}

echo(newhelloWorld)->hello()."\n";

Variadic Functions via "..."

Earlier we were using func_get_args() to get all the arguments available in a function call, but with PHP 5.6, this can be removed as we can easily get that facility with the... operator.

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

functionmyTools($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', 'PHPStorm');

// Output:

// Name: Avinash

// My Tool Count: 3

Argument Unpacking

We can use the same operator (...) to unpack any argument which is either an array or a set of Traversable objects.

01

02

03

04

05

06

07

08

09

10

11

12

13

14

functionmyTools($name, $tool1, $tool2, $tool3) {

echo"Name:". $name.'<br />';

echo"Tool1:", $tool1.'<br />';

echo"Tool2:", $tool2.'<br />';

echo"Tool3:", $tool3;

}

$myTools= ['Eclipse', 'Sublime', 'PHPStorm'];

myTools('Avinash', ...$myTools);

// Output:

// Name: Avinash

// Tool1: Eclipse

// Tool1: Sublime

// Tool1: PHPStorm

** Shorthand

The ** operator has been added for exponentiation. We have support for the shorthand operator as easily.

1

2

3

4

5

6

7

8

9

echo2 ** 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:

1

echo2 ** 2 ** 4;

You might expect it to return 256 as the grouping would be like (2 ** 2) ** 4, but that is not the case here. The real result would be 65,536, as the grouping would be from right to left and it will parse as 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 a SAPI module.

__debugInfo()

A new magic method added in PHP 5.6 allows you to change the properties and values of an object when the object is output using var_dump().

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

classdemoDebugInfo() {

private$val;

publicfunction__construct($val) {

$this->val = $val;

}

publicfunction__debuginfo() {

return[

'iamchanged'=> $this->val + 3;

];

}

}

$debugInfo= newdemoDebugInfo(50);

// We will get altered array which is returned by __debuginfo megic method.

var_dump($debugInfo);<b>

</b>

Default Character Encoding

The default character set for the htmlentities(), html_entity_decode() andhtmlspecialchars() functions can be set using default_charset().

Large File Upload

It is possible to upload a file larger than 2 GB.

php://input is reusable

php://input can be used as many times you want to read data. This feature offers a great reduction in memory compared to reading POST data.

use function and use const

The use operator has been offered to support the extending of constants and functions. This can be performed by using the const and the use functions, respectively. Earlier this operator was limited to class only.

Deprecated Features

Call From Incompatible Context

When you attempt to access a non-static method statically, or a static method using object context, it will immediately generate the E_DEPRECATED error.

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

classhelloWorld() {

publicstatichiWorld () {

}

}

$hello= newhelloWorld();

$hello->hiWorld();

// Before PHP 5.6

E_STRICT error

// From PHP 5.6

E_DEPRECATED error<b>

</b>

$HTTP_RAW_POST_DATA

$HTTP_RAW_POST_DATA is deprecated now. We should be using php://input instead.

Encoding Setting

With the launch of the default_charset() configuration, the related option is deprecated for iconv and mbstring.

List of Changed Functions

A list of all changed functions in PHP 5.6 can be found in the latest PHP Manual.

Conclusion

While the PHP team is working on the next version of PHP, I would say PHP 5.6 has shipped with a solid amount of improvements and feature additions, and it offers one of the easiest upgrades from past versions.