New features in PHP 8
Conversion Work
One of the major innovations in PHP is intended to give the scripting language a boost, but it does require some prior knowledge. As of version 5.5, the code to be executed has been stored in a cache provided by the OPcache extension [1]. Thanks to this cache, the interpreter does not have to reread the scripts for every request.
PHP 8 sees OPcache expanded to include a just-in-time (JIT) compiler. This converts parts of the PHP code into native program code, which the processor then executes directly and far faster as a consequence. As a further speed tweak, the generated binary code is also cached. When the PHP code is restarted, PHP 8 simply accesses the precompiled binary code from the cache.
As the first benchmarks by PHP developer Brent Roose show [2], scripts with repetitive or extensive calculations obviously benefit from these changes. When handling individual short requests, the JIT compiler shows its advantages to a far lesser extent.
Abbreviation
The syntax, which has changed slightly in some places in PHP 8, saves developers some typing. For example, the scripting language adds a more powerful and more compact match
function to supplement switch
. In Listing 1, $flasher
is only On
if $lever
is set to Up
or Down
. If none of the comparison values before =>
are correct, the default
value applies.
Listing 1
match
$flasher = match ($lever) { 0 => "Warning_Flasher", 'Up', 'Down' => "On", default => "Off" };
The call to $user->address->getBirthday()->asString()
in Listing 2 only works if $address
exists and getBirthday()
returns a valid object. To do this, developers previously had to nest several if
branches. The new nullsafe operator ?->
checks the existence automatically and reduces the test to one line (line 15).
Listing 2
Happy Birthday?
01 // ---- previously -------- 02 if ($user !== null) { 03 $a = $user->address; 04 05 if ($a !== null) { 06 $b = $user->getBirthday(); 07 08 if ($b !== null) { 09 $bday = $b->asString(); 10 } 11 } 12 } 13 14 // ---- in PHP 8 -------- 15 $bdate = $user?->address?->getBirthday()?->asString();
Where a class needs to encapsulate an address or other data, the developer usually first notes the appropriate variables, which are then assigned values by a constructor (Listing 3, lines 2-12). In PHP 8, this can be written in a concise way (Listing 3, lines 15-21).
Listing 3
Encapsulating Data
01 // ---- previously -------- 02 class Address 03 { 04 public string $name; 05 public DateTimeImmutable $birthday; 06 07 public function __construct(string $n, DateTimeImmutable $g) 08 { 09 $this->name = $n; 10 $this->$birthday = $g; 11 } 12 } 13 14 // ---- in PHP 8 -------- 15 class Address 16 { 17 public function __construct( 18 public string $name, 19 public DateTimeImmutable $birthday, 20 ) {} 21 }
The constructor now collects all the required information from which PHP 8 automatically derives the complete class structure. Thanks to the Constructor Property Promotion syntax, programmers can create data objects far faster and also refactor them more quickly later.
Exceptional Phenomenon
Until now, PHP developers always had to catch exceptions in a variable, even if they didn't want to do anything else with the corresponding object. From PHP 8 on, in such situations, you simply omit the variable and just specify the type. If you want to handle all errors in the catch
block, simply catch Throwable
(Listing 4).
Listing 4
Throwable
try { [...] } catch (Throwable) { Log::error("Error!"); }
The get_class()
function returns an object's class name. Developers using PHP 8 or higher can access this class with an appended ::class
, as shown in Listing 5.
Listing 5
Class Name
$a = new Address(); var_dump($a::class);
Typical!
Functions expect their parameters in a given order. Especially with a large number of parameters, it is easy to lose track. The following code, which calculates the volume of a box, for example, does not make it entirely clear which value stands for the width:
$v = volume(10, 3, 2);
In PHP 8, developers can explicitly note the names of the corresponding parameters to ensure clarity. When using these named arguments, you also choose the order of the parameters according to your needs. Optional parameters can also be omitted, like $depth
in the example shown in Listing 6. By the way, Listing 6 shows a further innovation: After the last parameter, you can use another comma, even if no further parameters follow.
Listing 6
Named Parameters
function volume(int $width, int $height, int $depth = 1) { return $width * $height * $depth; } $v = volume(height: 3, width: 10,);
Buy this article as PDF
(incl. VAT)