1.2.861. Null On New

Until PHP 7, some classes instantiation could yield null, instead of throwing an exception.

After issuing a ‘new’ with those classes, it was important to check if the returned object were null or not. No exception were thrown. This inconsistency has been cleaned in PHP 7 : see See Internal Constructor Behavior

<?php

// Example extracted from the wiki below
$mf = new MessageFormatter('en_US', '{this was made intentionally incorrect}');
if ($mf === null) {
    echo 'Surprise!';
}

?>

See also PHP RFC: Constructor behaviour of internal classes.

1.2.861.1. Suggestions

  • Remove the check on null after a new instantiation

1.2.861.2. Specs

Short name

Classes/NullOnNew

Rulesets

All, CompatibilityPHP53, CompatibilityPHP54, CompatibilityPHP55, CompatibilityPHP56

Exakat since

0.8.4

PHP Version

With PHP 7.0 and older

Severity

Major

Time To Fix

Instant (5 mins)

Precision

Very high

Features

class, new, null

Available in

Entreprise Edition, Exakat Cloud