Skip to content

ForbiddenGlobalVariableVariable false positive #537

@glensc

Description

@glensc
<?php

$client_id = "dcid";
$dcid = 123;

function f() {
        global $client_id;
        global $$client_id;

        return $$client_id;
};

var_dump(f());
#> int(123)

Global with variable variables is not allowed since PHP 7.0
(PHPCompatibility.PHP.ForbiddenGlobalVariableVariable.Found)

added via #110, bugfixed #316

however, this code is valid for php 5.x and 7.x (tested):

$ php53  -r '$client_id="dcid"; $dcid=123; $f=function() {global $client_id; global $$client_id; var_dump($$client_id); return $$client_id; }; $f();'
int(123)

$ php72  -r '$client_id="dcid"; $dcid=123; $f=function() {global $client_id; global $$client_id; var_dump($$client_id); return $$client_id; }; $f();'
int(123)

the behavior for references changed, but it is not deprecated:
http://php.net/manual/en/migration70.incompatible.php#migration70.incompatible.variable-handling.global

global only accepts simple variables
Variable variables can no longer be used with the global keyword. The curly brace syntax can be used
to emulate the previous behaviour if required:

function f() {
    // Valid in PHP 5 only.
    global $$foo->bar;

    // Valid in PHP 5 and 7.
    global ${$foo->bar};
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions