Norman Bird
Norman Bird

Reputation: 682

PHP function written in 5.5 throws error when upgraded to 7.0

here is the function that worked prior to upgrading to 7.0

function set_session_vars() {
$nb_args=func_num_args();
$arg_list=func_get_args();
for($i=0;$i<$nb_args;$i++) {
    global $$arg_list[$i];
    $_SESSION[$arg_list[$i]] = $$arg_list[$i];
}

}

now it causer error that says:

Parse error: syntax error, unexpected '[', expecting ',' or ';' in /home/mvyc1956/public_html/members/includes/functions.php on line 322

I believe its related to non backward compatable changes to GLOBAL and the use of $$ and arrays, but my PHP is not good enough to figure it out.

Is there someone who is familiar with why this line :

global $$arg_list[$i];

which is line 322 that is being reported as the cause of the error, would be failing now, and what would you recommend I change the code to in order to have it work with PHP 7?

I did some googling and found this page but again, im not understanding what needs to be changed.

thanks

says syntax error so some of the code in the function is no longer valid, but it would take a php 7 expert to see it.

UPDATE I removed the word GLOBAL from the above code and the app "seems" to be now working fine so I will now ask:

Does anyone know specifically, why Global was the non compatibility issue? and is my fix of simply removing it a solid one or will is there a better practice or will this removal come back to haunt me?

Upvotes: 0

Views: 108

Answers (2)

dgatwood
dgatwood

Reputation: 10407

The global keyword tells PHP to access a global variable (per launch of the script) instead of a local variable (per function). For example,

global $foo;

means that future uses of the variable $foo in that function refer to the global variable with that name, rather than a variable within the function itself.

What this is trying to do is look up a variable by arbitrary name in the global namespace. That's entirely the wrong way to do things. Instead, you should have a global array and use keys in the array. In fact, $$ is arguably a bad idea in general.

But that's neither here nor there. The problem is that the parsing rules changed in PHP 7.0 in a non-backwards-compatible way (because they're using a more traditional parser now, and thus had to make their associativity rules self-consistent).

More details here: http://php.net/manual/en/migration70.incompatible.php#migration70.incompatible.variable-handling.indirect

To make a long story short, you need to rewrite that as:

global ${$arg_list[$i]};

and then your code will work correctly on both PHP 7 and PHP 5.

Incidentally, the function only appears to work without the global keyword. In fact, it is always getting empty values for those variables.

Upvotes: 2

Remus Rusanu
Remus Rusanu

Reputation: 294287

Backward incompatible changes:

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:

// Valid in PHP 5 only.
global $$foo->bar;

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

So in your case it should become:

global ${$arg_list[$i]};

Upvotes: 2

Related Questions