Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

What is new in PHP?

Monday, May 2nd, 2016 by Servage

php7What is new in PHP?

Chances are you have been developing dynamic web applications using PHP 5. After a long wait and a series of issues with PHP 6, PHP 7 is finally making its way to the hands of developers who are eagerly taking advantage of the new features it has to offer. But what does it have to offer? What are the new key features in PHP 7? That is what we are going to find out in this article, so you know what benefits you may get from a transition later on.

Return type declarations

The first new feature on the list is return type declarations. Return type declarations allow you to specify what type of data can be expected back from a function. A return type can be declared by using a colon followed by a type after the parameters the function accepts. As an example, the following function returns an integer: function multiply($a, $b): int { return $a * $b; }.

New type hints

Type hints were available in PHP 5 already, but in PHP 7 new type hints have been introduced. Type hints are a way to declare what arguments a function takes as input. For example, the following function takes strings as inputs and returns them concatenated: function addStrings(string $a, string $b){ return $a . $b; }.

String is one of the new type hints in PHP7, in addition to integers, floats and booleans. Type hints are helpful to enforce input consistency, as PHP 7 throws a TypeError exception if the input type does not match what the function expects to receive.

Anonymous classes

PHP 7 introduces anonymous classes, which can be used to create one-time classes and assign them to variables. Anonymous classes are helpful when a small object is needed only once within an application. If a class is instantiated multiple times, it may be better to use a normal class instead.

An anonymous function can be created using the new keyword followed by Class() For instance: $anonymousClass = new Class() { }. The parentheses are not required here if the class doesn’t have any parameters.

Two new operators

Two new operators have been added to PHP 7: the null-coalescing operator and the spaceship operator. The null-coalecsing operator is a short-hand way to check if a variable is null or does not exist. The syntax for the operator is: $ObjectName = $obj->name ?? “Unnamed Object”.

The spaceship operator can be used to check if a variable is less than, equal to or greater than another variable or value. It returns values between -1 and 1 depending on what the result from the comparison is.

Performance improvements

Last but not least, PHP 7 brings significant performance improvements. According to some benchmarks, applications have gained up to 100% performance improvements.

A thing to keep in mind

PHP 7 is a new major release, and major PHP releases are allowed to break backward-compatibility. This is a good thing to keep in mind when upgrading PHP 5 applications to run on PHP 7.

What is new in PHP?, 4.7 out of 5 based on 3 ratings
Categories: Guides & Tutorials

Keywords:

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.