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

5
  • What does var_dump($_SESSION["payment"]) show? Commented Jun 3, 2016 at 20:40
  • Just a precision : Before the data was in a simple files and now the same datas are in class. Commented Jun 3, 2016 at 20:42
  • var_dump($_SESSION['payment']); See above Commented Jun 3, 2016 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." Commented Jun 3, 2016 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. Commented Jun 3, 2016 at 21:46

1 Answer 1

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}));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.