In 1st March 2012, new version of PHP has been released. Some awesome features has been added whereas some deprecated functionalities are removed. Lets discuss about the new interesting features of PHP 5.4.
-
- Traits : A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies. In essence, traits are similar to abstract classes and can contain any number of properties and methods. The best use of traits is demonstrated when multiple classes share the same functionality.
- Built-In web server : PHP 5.4 offers a built-in web server which runs from the Windows, Mac or Linux command line. Apache is much popular as web server, but it would be nice if there is tiny and simple web server that can be used with simple command line.
- Short array syntax – It’s now possible to use JavaScript-like square brackets rather than using the old array() construct,
for example
$myArray1 = [1, 2, 3]; $myArray2 = [ "one" => "first", "two" => "second", "three" => "third" ];
- Short Open tag in default : <?=$myVariable;?> is always available regardless of how short_open_tag is set in ini file of php.
- Binary number format : Binary number system is added in the new version of PHP. For example 0b01101000
- Array Dereferencing : There is simpler array dereferencing. For example
$myElement = getArray()[1]
- $this in closures(anonymous functions) : We can now refer to the object instance by using $this from anonymous functions which also called closures. Closures allow the creation of functions which have no specified name. They are most useful as the value of callback parameters, but they have many other uses. It can also be used as the value of variables. In addition, it may also inherit variables from the parent scope.
- Callable Typehint : The typehint would allow a string with a function name, an array composed of classname/obj+methodname, and a closure. It is for those who desire to make PHP a stronger typed language. Type Hints can only be of the object and array type since PHP 5.1, and callable since PHP 5.4. For example
function hi(callable $f) { $f(); } hi([new Human("Peter"), 'hello']);
- Session Upload Progress : When the session.upload_progress.enabled INI option is enabled, PHP will be able to track the upload progress of individual files being uploaded. This information isn’t particularly useful for the actual upload request itself, but during the file upload an application can send a POST request to a separate endpoint (via XHR for example) to check the status.
- Class member access on instantiation : Class members can now be accessed on instantiation in PHP 5.4. This is a very handy addition. Before PHP 5.4 you had to create an object into a variable, and then access the class member/function. For example.
//Old way $object = new MyClass(); $object->myFunction(); //New way (new MyClass)->myFunction();
- Class::{expr}() syntax : It also one important feature added in PHP 5.4. For example.
foreach ([new Human("John"), new Human("Bradd")] as $human) { echo $human->{'hello'}(); }
- Messages and warnings : There is improved parse error messages and improved incompatible arguments warnings in the newer version of PHP 5.4
Removed features:
- Removed break/continue $var syntax : We can no longer use variable to tell PHP how many levels of enclosing loops it should skip to the end of.
- Removed safe mode and all related ini options : Functionality related with safe modes and security are removed in PHP 5.4 which were marked as depreciated in PHP 5.3
- Removed register_globals and register_long_arrays ini options : If enabled, register_globals injected PHP scripts with all sorts of variables, like request variables from HTML forms or values from GET requests. Now, every request/environment variable must be fetched from an appropriate PHP array.
- Removed allow_call_time_pass_reference option : Passing arguments by reference at function call time was deprecated for code-cleanliness reasons. A function can modify its arguments in an undocumented way if it didn’t declare that the argument shall be passed by reference. To prevent side-effects it’s better to specify which arguments are passed by reference in the function declaration only.