New features in PHP 7
Optimization Conduct
As you go through your scripts, you can install some of the new features of PHP 7 in a suitable place and check your changes directly in the code using the assert()
function. If you add
ini_set('zend.assertion',-1)
PHP 7 passes over the function calls to assert()
in production mode, in this mode the function calls are dropped, which reduces the overhead. The new coalesce operator (??
) designs the program code so it is easier to read and saves typing (Listing 5, line 3).
Listing 5
New Coalesce Operator
The new combined comparative operator (<=>
), also known as the spaceship operator, has a similarly elegant effect in the program code. It works dually and compares two values in one fell swoop for the operators >
, <
, and =
. Evaluating from left to right, PHP 7 assesses the equality (1 <=> 1
) as 0
; a smaller value, left to right, (0 <=> 1
) as -1
, and a larger value (1 <=> 0
) as 1
. The operator can take advantage of the lexical order in the set of all characters to make similar character strings. The expression "foo" <=> "bar"
is evaluated as 1
.
To save space, you can condense use
statements as in the following:
use foo\bar{Class Foo, Class Bar as FooBar}
PHP 7 also provides the option to promote fields to constants using define
and to represent Unicode characters using an escape syntax, such as "\u{af}"
in strings. The script language encodes individual characters using their hexadecimal code in the Unicode standard.
Culture Shock
The use of type declarations in functions is probably the most striking new feature in PHP 7. Using this mechanism, which is also downward compatible, PHP users can stipulate the data types for both the call parameter and the return values, as demonstrated in Listing 6. The
declare(strict_types=1)
Listing 6
Strict Adherence to Data Types
statement in line 2 activates the type declaration. The ...
operator before the $ints
parameter in line 4 shifts all call parameters to the $ints
field. The type declaration applies to all the call parameters.
If line 2 were omitted, line 4 would add the calls for the sum
function in lines 8 and 9 using the array_sum()
function as usual for a return value of 6
. However, because of line 2, both calls fail. Instead, the code generates a TypeError exception from line 4 for lines 8 and 9. The strict_types
declaration affects code that PHP incorporates via require
or include
statements.
Throwaway Society
PHP 7 generates one-off objects using anonymous classes (Listing 7). Apparently, the joint years with JavaScript didn't pass by without a trace. Line 6 creates an empty object from the anonymous class {}
. Like anonymous functions, anonymous classes don't need a name; developers just specify them in an assignment immediately after the new
operator.
Listing 7
Anonymous Classes
Line 8 shows how PHP 7 expands the empty object $Prototype
at run time with the calculator
property and stores the instance of an object that the script previously instantiated with the anonymous class. This class implements the interface
from line 2, and line 9 defines the required function result()
. The expression in the last line of the listing, which returns a value of 4, demonstrates the efficiency of the construct.
« Previous 1 2 3 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
So Long Neofetch and Thanks for the Info
Today is a day that every Linux user who enjoys bragging about their system(s) will mourn, as Neofetch has come to an end.
-
Ubuntu 24.04 Comes with a “Flaw"
If you're thinking you might want to upgrade from your current Ubuntu release to the latest, there's something you might want to consider before doing so.
-
Canonical Releases Ubuntu 24.04
After a brief pause because of the XZ vulnerability, Ubuntu 24.04 is now available for install.
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.