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
-
Endless OS 6 has Arrived
After more than a year since the last update, the latest release of Endless OS is now available for general usage.
-
Fedora Asahi 40 Remix Available for Macs with Apple Silicon
If you've been anticipating KDE's Plasma 6 for your Apple Silicon-powered Mac, then you're in luck.
-
Red Hat Adds New Deployment Option for Enterprise Linux Platforms
Red Hat has re-imagined enterprise Linux for an AI future with Image Mode.
-
OSJH and LPI Release 2024 Open Source Pros Job Survey Results
See what open source professionals look for in a new role.
-
Proton 9.0-1 Released to Improve Gaming with Steam
The latest release of Proton 9 adds several improvements and fixes an issue that has been problematic for Linux users.
-
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.