1.2.688. Methodcall On New

It is possible to call a method right at object instantiation.

This syntax was added in PHP 5.4+. Before, this was not possible : the object had to be stored in a variable first. This syntax is interesting when the object is not reused, and may be discarded

<?php

// Data is collected
$data = data_source();

// Data is saved, but won't be reused from this databaseRow object. It may be ignored.
$result = (new databaseRow($data))->save();

// The actual result of the save() is collected and tested.
if ($result !== true) {
    processSaveError($data);
}

?>

1.2.688.1. Connex PHP features

1.2.688.1.1. Specs

Short name

Php/MethodCallOnNew

Rulesets

All, Changed Behavior, CompatibilityPHP53

Exakat since

0.8.4

PHP Version

With PHP 5.4 and more recent

Severity

Major

Time To Fix

Quick (30 mins)

Changed Behavior

PHP 5.4

Precision

Very high

Available in

Entreprise Edition, Exakat Cloud