1.2.1191. Throw Was An Expression

Throw used to be an expression. In PHP 7.0, there were some location where one couldn’t use a throw : this was the case for arrow functions, which expect one expression as function’s body.

Using throw as an instruction makes the code incompatible with PHP 7 version and older.

<?php

// Valid in PHP 8.0 and more recent
$fn = fn($a) => throw new Exception($a);

?>

See also Throw Expression and Exceptions.

1.2.1191.1. Specs

Short name

Php/ThrowWasAnExpression

Rulesets

All, CE, Changed Behavior, CompatibilityPHP72, CompatibilityPHP73, CompatibilityPHP74, One Liners

Exakat since

2.1.1

PHP Version

With PHP 8.0 and more recent

Severity

Major

Time To Fix

Quick (30 mins)

Changed Behavior

PHP 8.0 - More

Precision

Very high

Features

throw

Available in

Entreprise Edition, Community Edition, Exakat Cloud