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.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Rust Language

    We look at a few features of Rust, Mozilla's systems programming language, and its similarity to other languages.

  • ECMAScript 6

    The new ECMAScript 6 language eliminates many historical problems associated with JavaScript.

  • Dotnet Scripting with Boo

    Boo is a scripting language tailor-made for Mono and .NET. This haunting mixture of Python and C# may be just what you need to get started with the .NET framework.

  • Python generators simulate gambling

    Can 10 heads in a row really occur in a coin toss? Or, can the lucky numbers in the lottery be 1, 2, 3, 4, 5, 6? We investigate the law of large numbers.

  • Pony Programming Language

    Pony, an object-oriented programming language with static typecasting, trots down well-mapped paths to deliver secure, high-performance code for concurrent applications.

comments powered by Disqus
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.

Learn More

News