0

Before upgrading to PHP 7, I had this code and it returned true

var_dump(isset($$_SESSION['payment']) );
var_dump(is_object($$_SESSION['payment'])); 
var_dump($_SESSION['payment']); // string 'moneyorder'

After upgrading to PHP 7, I rewrote the same code inside a class, but now it returns false

var_dump(isset(${$_SESSION['payment']})); 
var_dump(is_object(${$_SESSION['payment']}));
var_dump($_SESSION['payment']); // string 'moneyorder'

Do you have an idea why ?

Thank you

miken32
  • 42,008
  • 16
  • 111
  • 154
kurama
  • 677
  • 3
  • 8
  • 16
  • What does `var_dump($_SESSION["payment"])` show? – miken32 Jun 03 '16 at 20:40
  • Just a precision : Before the data was in a simple files and now the same datas are in class. – kurama Jun 03 '16 at 20:42
  • var_dump($_SESSION['payment']); See above – kurama Jun 03 '16 at 20:46
  • So does your original code run in PHP 7 or not? Because it seems like your question should say "After upgrading to PHP 7, rewriting some lines of code, and putting them in a class, it works differently." – miken32 Jun 03 '16 at 20:49
  • I suggest you redesign your code so you don't need variable variables. Anything you do with them should probably be done using a associative array. – Barmar Jun 03 '16 at 21:46

1 Answers1

1

Note the PHP documentation for superglobals contains this warning:

Note: Variable variables

Superglobals cannot be used as variable variables inside functions or class methods.

Save it to a local variable instead:

$payment = $_SESSION['payment'];
var_dump(isset(${$payment})); 
var_dump(is_object(${$payment}));
miken32
  • 42,008
  • 16
  • 111
  • 154