PHP 7 – what’s new and feature spotlight

Published on: Feb 7, 2016 7:55 pm  /   Published by:   /   Comments:

You might already know that PHP 7 is now a stable release. PHP 7 is a new major version, still it has very few backward incompatible changes. However, lots of functions have been deprecated. This is the most important version update for PHP since the release of PHP 5 in the year of 2004. Drastic performance improvements along with reduced memory consumption will supercharge your web application. The new Zend Engine 3.0 enables your website with 2x faster performance and 50% better memory consumption than PHP 5.6 which allows you to serve more concurrent visitors without adding a new hardware. PHP 7 is based on the PHPNG AKA PHP Next-Gen project, that was led by Zend. Also, PHP 7 now has support for 64-bit Windows system which was lacking in 5.x version. However, it was already supported in the Linux 64-bit platforms and local LAMP stacks. Besides the performance enhancements, PHP 7 has introduced many new features, like new operators, type declarations, anonymous classes, namespace imports, etc.

It’s PHP 7 after PHP 5.6 – Why the version jump?

The last stable release of PHP before the version 7 was the version 5.6! Actually, PHP 6 was in the development phase in the past as an experimental project, but it never entered into the production phase. The PHP development team decided to drop the version 6 after some voting system.

PHP 7 Zend Infographic

PHP 7 Infographic, Graphic Curtesy: – Zend.com

New Operators

null coalescing operator ( ?? )

This new operator has been added to simplify the inline if/else syntax with the ternary operator. So rather than writing this

$var = !empty($_POST['some_var']) ? $_POST['some_var'] : 0;
Now you can write this
$var = $_POST['some_var'] ?? 0;
This can be chained as well
$var = $_POST['some_var'] ?? $_POST['another_var'] ?? 0;

spaceship operator ( <=> )

This is another new operator added in PHP 7. It compares two expressions and returns -1, 0, or 1 respectively for less than, equals, or greater than for the match.

<?php
// Integers
echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1
// Floats
echo 1.5 <=> 1.5; // 0
echo 1.5 <=> 2.5; // -1
echo 2.5 <=> 1.5; // 1
// Strings
echo "a" <=> "a"; // 0
echo "a" <=> "b"; // -1
echo "b" <=> "a"; // 1
?>

New classes and interfaces

There are a couple of new classes and interfaces introduced. Especially the error handling part. The Exception Hierarchy classes are as below which are very useful.

  • Throwable
  • Error
  • TypeError
  • ParseError
  • AssertionError
  • ArithmeticError
  • DivisionByZeroError

New functions

There are a couple of new functions introduced in PHP 7 version.

ClosureClosure::call() to bind and call the closure function.
Mathintdiv() function for integer division. This is a very handy function.
Error Handlingerror_clear_last() to clear the most recent error.
Resourcesget_resources() to get the list of the resources created by type.
Zlib Compression – inflate_add(), deflate_add(), inflate_init(), deflate_init()

Modified functions

Some existing functions are modified. Like –
dirname() – now optionally takes a second parameter, depth, to get the name of the directory depth levels up from the current directory.
mktime() no longer accepts is_dst (day light saving) parameter.
preg_replace() function no longer supports “\e” (PREG_REPLACE_EVAL). preg_replace_callback() should be used instead.
exec() function has NULL byte protection now.
substr() now returns an empty string, if string is equal to start characters long.

New Global Constants

Reference:

Core Predefined Constants
PHP_INT_MIN

LibXML
LIBXML_BIGLINES

PCRE
PREG_JIT_STACKLIMIT_ERROR

POSIX
POSIX_RLIMIT_AS, POSIX_RLIMIT_CORE, POSIX_RLIMIT_CPU, POSIX_RLIMIT_DATA, POSIX_RLIMIT_FSIZE, POSIX_RLIMIT_LOCKS, POSIX_RLIMIT_MEMLOCK, POSIX_RLIMIT_MSGQUEUE, POSIX_RLIMIT_NICE, POSIX_RLIMIT_NOFILE, POSIX_RLIMIT_NPROC, POSIX_RLIMIT_RSS, POSIX_RLIMIT_RTPRIO, POSIX_RLIMIT_RTTIME, POSIX_RLIMIT_SIGPENDING, POSIX_RLIMIT_STACK, POSIX_RLIMIT_INFINITY

Zlib
ZLIB_BLOCK, ZLIB_FINISH, ZLIB_FULL_FLUSH, ZLIB_NO_FLUSH, ZLIB_PARTIAL_FLUSH, ZLIB_SYNC_FLUSH

Others

Timezone warning removed

Previously, a warning was emitted if the date.timezone INI setting had not been set prior to use any date- or time-based functions. Now, this warning has been removed (with date.timezone still defaulting to UTC).

Loosening Reserved Word Restrictions

There was a restriction of using globally reserved words as property, constant, and method names within the classes, interfaces, and traits. But those are now allowed.

<?php
// 'new', 'private', and 'for' were previously unusable
Project::new('some_name')->private()->for('some_purpose')->with('some_parameter');
?>

The “class” keyword still cannot be used for the above purposes.

Deprecations

PHP 4 style constructors

Methods defined with the name same as the class name is treated as the constructor, but no more! This is deprecated in PHP 7 and will be removed in the future releases. The PHP engine will emit E_DEPRECATED in this type of cases.
<?php
class foo {
function foo() {
echo 'this is the constructor method';
}
}
?>

Static calls to non-static methods

Static calls to methods that are not declared static are deprecated, and may be removed in the future.
<?php
class foo {
function bar() {
echo 'Not a static method!';
}
}
foo::bar();
?>