1.2.971. Php 8.0 Variable Syntax Tweaks

Several variable syntaxes are added in version 8.0. They extends the PHP 7.0 syntax updates, and fix a number of edges cases.

In particular, new``and ``instanceof now support a way to inline the expression, rather than use a temporary variable.

Magic constants are now accessible with array notation, just like another constant. It is also possible to use method calls : although this is Syntacticly correct for PHP, this won’t be executed, as the left operand is a string, and not an object.

<?php

 // array name is dynamically build
 echo "foo$bar"[0];
 // static method
 "foo$bar"::baz();
 // static property
 "foo$bar"::$baz;

 // Syntactly correct, but not executable
 "foo$bar"->baz();

 // expressions with instanceof and new
    $object = new ("class_".$name);
    $x instanceof ("class_$name");

    // PHP 7.0 style
    $className = "class_".$name;
    $object = new $className;

?>

See also PHP RFC: Variable Syntax Tweaks and scalar_objects in PHP.

1.2.971.1. Specs

Short name

Php/Php80VariableSyntax

Rulesets

All, Appinfo, CE, CompatibilityPHP74

Exakat since

2.0.8

PHP Version

With PHP 8.0 and more recent

Severity

Major

Time To Fix

Quick (30 mins)

Precision

Very high

Features

variable

Available in

Entreprise Edition, Community Edition, Exakat Cloud