PHP 5.4 has been released for quite a bit time and I thought of checking out what’s new and what has changed. Below is a short list of things to checkout for in the new 5.4 version.

Basically new features have been added that will at least save a bit of time while typing. It was nice to see some basic features present in other languages like Java, JavaScript being added to PHP.

Short array syntax has been added.

It is now possible to use square brackets to  define an array instead of using array().

Function array dereferencing has been added

I always wondered if this will ever be added in PHP and glad that it is now available in PHP from 5.4. Previously if any function was returning an array, you were required to store it in a variable and then access the array elements using that variable. Now starting from 5.4 you can directly access the returned array from the function call itself as you can in Java and JavaScript.

Echoing is now easier

Want to echo just a variable in side html? Well, it has become easier now.  Write no more the lengthy <?php echo $x; ?> as the short hand expression <?= is now always available, regardless of the short_open_tag php.ini option.

Class member access on instantiation has been added

What do you think about the echo statement in the following code? Isn’t it quick to use rather than assigning the new instance to a variable and then using that to call the function?

Well, sure it’s quicker but if you run it on server with PHP version less than 5.4 you will get the following error:

Parse error: syntax error, unexpected T_OBJECT_OPERATOR on line 8

But, if you run it on PHP 5.4 you will be greeting hello to PHP 5.4 as it will parse successfully and output the below line:

Hello PHP 5.4

$this is now supported inside closures

If you use the $this inside a closure present inside a class to access any property of the class , it will give error in PHP version less than 5.4. Consider the below code:

In PHP 5.3 you’ll get the below error:

Fatal error: Using $this when not in object context on line 8

But in PHP 5.4 you will see the year 2012 echoed out successfully;

Class::{expr}() syntax is now supported

Suppose for some reason you dynamically decide the name the static function of a class to call. Consider the code below:

In the above code the name of the static function to be called is obtained by concatenating 2 variables. But, as this expression for calling static function is not supported prior to version 5.4, you will get the following error:

Parse error: syntax error, unexpected ‘{‘, expecting T_STRING or T_VARIABLE or ‘$’ on line 17

But if you run it on 5.4 you’ll successfully see

Binary number format has been added

We know we can use octal and hexadecimal number formats, so now from PHP 5.4 we can also use the binary number format.  Refer the below code:

It will successful output 18 three times when run in PHP 5.4, but will give the following error on lower version :

Parse error: syntax error, unexpected T_STRING  on line 5

Built-in development web server in CLI mode.

A web server now ships built in with PHP 5.4.  In the command line you can start the server with

php -S <ip address>:<port> e.g. php -S 127.0.0.1:8800

Starting the built in server in php5.4 from the shell

You can start the php internal server from any directory you like.  Just start the command line from that directory or change  prompt to that directory.  The directory you start it in will act as the Document Root. Below is a screen shot of file being served from the server.

file_running_on_php5.4_built_in_server

Improved parse error messages and improved incompatible arguments warnings.

Suppose I run a file containing the following piece of code

function returnYear 2012

There is syntax error in the above code as parentheses should have followed the function name. So when you run the code in PHP versions less than 5.4 you get the following error:

Parse error: syntax error, unexpected T_LNUMBER, expecting ‘(‘  on line 2

But when you run it in PHP 5.4 the error message is slightly improved. It not only displays the error but also points out which token or code piece in the line is causing the error. So it will save time where instead of looking through out the line you can directly get to the point which causes the error.

Parse error: syntax error, unexpected ‘2012’ (T_LNUMBER), expecting ‘(‘ on line 2

Also in previous versions when you try to echo a variable which is an array, you see the string Array being echoed out.  Starting from PHP 5.4 it will raise a notice as an array is not supposed to be echoed as a String.

 

A NEW LANGUAGE FEATURE:

Traits:

Ok, let me ask you a question. Tell me how will you inherit methods from more than once classes in PHP? And you say, hey! what are you talking about? There are no multiple inheritances in PHP. Hmm, you are right. However starting from PHP 5.4 you will be able to use methods from multiple classes; well, technically multiple Traits. This trait feature has been added to increase code reusability in the absence of multiple inheritances. The traits are like classes except from the fact that you cannot instantiate them. A class can use multiple traits gaining the ability to use methods from more than once classes (traits). So the basic usage of Trait is to enhance code reuse. Below is an example:

NEW FUNCTIONS ADDED

 

Please visit this link http://www.php.net/manual/en/migration54.php if you would like to know other changes