-
-
Notifications
You must be signed in to change notification settings - Fork 205
Closed
vfalies/php7compatibility
#2Description
<?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)
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};
}Reactions are currently unavailable