Blogs

PHP7 THE NEXT MAJOR THING

Sep 10, 2016

PHP 7

PHP 7 was recently released. The 7.0.0 major release contains so many cool features that we decided to dedicate a whole post to it.
1. The Brand Spanking New Zend Engine

The Zend engine has been powering PHP since 1999 when it was introduced with the then new PHP 4 release. Zend – not to confuse with the Zend Framework – is an open-source execution engine written in C that interprets the PHP language. The current PHP 5.X series use Zend Engine II that enhanced the functionality of the initial engine and adds an extensible object model and asignificant performance enhancement to the language.
PHP 7 receives a brand new version of the engine, the code name of PHP#NG (Next Generation).
2. Twice the Speed
The most easily recognizable advantage of the new PHPNG engine is the significant performance improvement. The development team of PHPNG refactored the Zend Engine, and remarkably optimized memory usage.
The results? You can see the performance benchmarks provided by the Zend Performance Team below. By using PHP 7 not only your code will be executed faster but you will also need fewer servers to serve the same amount of users.

3. Facilitates Error Handling
To say the least, handling fatal and catchable fatal errors have never been an easy task for PHP coders. The new Engine Exceptions will allow you to replace these kind of errors with exceptions. If the exception is not caught, PHP will continue to return the same fatal errors as it does in the current 5.X series.
The new \EngineException objects don’t extend the \Exception Base Class. This ensures backward compatibility and results in two different kinds of exceptions in error handling: traditional and engine exceptions.
To enable programmers to catch both, PHP 7 introduces a new shared Parent Class under the name of \BaseException.
try {

nonExistentFunction();

} catch (\EngineException $e) {

var_dump($e);

}
4. 64-Bit Windows Systems Support
PHP is a prominent member of the LAMP stack which means its native environment is Linux – but it’s also possible to run it on a Windows system. The 5.X series don’t yet provide 64-bit integer or large file support, so until now x64 builds have been considered experimental.
PHP 7 will change this as it introduces consistent 64-bit support which means both native 64-bit integers and large files will be supported, allowing you to confidently run the language on your 64-bit Windows system in the future.

5. New Spaceship and Null Coalescing Operators
The Spaceship operator runs under the official name of Combined Comparison Operator. The notation of the new operator looks like this: <=> (kind of like a simplified spaceship, if you imagine it right).
The spacehip operator returns 0 if both operands are equal, 1 if the left is greater, and -1 if the right is greater. It’s also called a three-way comparison operator, and it already exists in other popular programming languages like Perl and Ruby.
// Integers
echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1

The Null Coalescing operator is denoted with two question marks ( ?? ). You can use it when you want to check if something exists and return a default value, in case it doesn’t. The coalesce operator returns the result of its first operand if it exists and is not null, and the second operand in any other cases.
Here’s how the new operator reduces the time spent with basic declarations:
// Fetches the request parameter user and results in 'nobody' if it doesn't exist
$username = $_GET['user'] ?? 'nobody';
// equivalent to: $username = isset($_GET['user']) ? $_GET['user'] : 'nobody';

6. Enables Accurate Type Declarations
Have you ever wanted to prevent unintended return values by declaring the return type of a function? Well, the new PHP 7 enables developers to enhance the quality of their code with the help of return type declarations.
The image below depicts a very simple use case where the foo() function is supposed to return an array. Check out more complicated examples here.
function foo(): array {
return [];
}

To enhance the feature even more, PHP 7 introduces 4 new type declarations for scalar types: int, float, string and bool. The new scalar types allow developers to denote that they are expecting integers, floats, strings, or booleans to be returned. The new scalar types introduced by PHP 7 will also be supported by argument Type Hints that enables developers to force the type of parameters since the PHP 5.X series.

7. Adds Anonymous Classes
PHP 7 enables you to use anonymous classes, already a well-established practice in other object-oriented languages like C# and Java. An anonymous class is a class without a name. The object it instantiates has the same functionality as an object of a named class.
The syntax is the same as what we are used to in traditional PHP classes, only the name is missing. If anonymous classes are used well, they can speed up coding as well execution time. Anonymous classes are excellent when a class is used only once during execution and in cases when a class doesn’t need to be documented.
var_dump(new class($i) {
public function __construct($i) {
$this->i = $i;
}
});

8. Facilitates Imports From the Same Namespace
The new Group Use Declarations feature will be godsent to those of you who want to import many classes from the same namespace. The new syntax cuts verbosity, makes your code tidier and easier on the eyes, and saves you a lot of typing time.
It will also be easier to read through and debug codes, as group use declarations help you identify the imports that belong to the same module.
use FooLibrary\Bar\Baz\{ ClassA, ClassB, ClassC, ClassD as Fizbo };

// Compared to current use syntax:

use FooLibrary\Bar\Baz\ClassA;
use FooLibrary\Bar\Baz\ClassB;
use FooLibrary\Bar\Baz\ClassC;
use FooLibrary\Bar\Baz\ClassD as Fizbo;

Group use declarations are used to cut verbosity when importing multiple entities from a common namespace. Using common PHP library examples, the following use declarations are equivalents:

// Current use syntax:

use Doctrine\Common\Collections\Expr\Comparison;
use Doctrine\Common\Collections\Expr\Value;
use Doctrine\Common\Collections\Expr\CompositeExpression;

use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion as Choice;
use Symfony\Component\Console\Question\ConfirmationQuestion;

// Proposed group use syntax:

use Symfony\Component\Console\{
Helper\Table,
Input\ArrayInput,
Input\InputInterface,
Output\NullOutput,
Output\OutputInterface,
Question\Question,
Question\ChoiceQuestion as Choice,
Question\ConfirmationQuestion,
};
9. Cleans Up the Room
The goal of PHP 7 was to free up the space to enable improvement, so it was necessary to get rid of many deprecated functionalities and old and unsupported Server APIs and extensions.
All the removed items have been deprecated for a while in PHP 5 so most likely you haven’t used them for a long time. However please note if you have a legacy app running on older PHP versions the new PHP 7 can potentially break the code.

About Author

Seasia Infotech is a CMMi5 certified company providing Software Development, Mobile Application Development and Digital Marketing services to businesses worldwide. We embrace the latest technologies to deliver solutions that address our clients’ business challenges. We focus on resolving the IT conundrum for our clients thereby enabling them to focus on their core business strengths.