1

I'm trying to fix a friend's web site that needs to be updated to run on php7.2. It works fine on php5 but appears to crash with no errors when executing a command like this:

$$mod_name = &$$parent_array[$mod_parent]->addItem(new XNode($mod_name,$mod_url,"Images/doc.gif",false));
 

The weird variable reference like &$$ is not something I've seen before. Has this notation been depreciated in php7?

The code is using a library called xPandMenu. Here is the library:

https://www.phpclasses.org/package/2018-PHP-Generate-a-dynamic-hierarchic-menu.html

I reached out to the author of this code and he's not interested in updating it, and doesn't work with PHP much any more. I am not familiar with OOP and the odd variable/class references used.

Does anybody know what would cause this code to work fine in php5, but crash without error in php 7.2?

SlackO
  • 41
  • 3
  • 1
    What does `crash without error` mean, a blank page, or what? – Paul T. Sep 17 '20 at 01:43
  • 1
    When I see code like `$$mod_name = &$$parent_array...` I walk away from the project immediately. Really bad code smell like that usually means debugging is going to be a nightmare. As for the blank page, make sure you have errors/warnings turned on - https://stackoverflow.com/a/21429652/296555 – waterloomatt Sep 17 '20 at 03:17
  • So it just stops... nothing in the error logs, but I put breakpoints before and after in the code so I know that's where it just crashes. I agree.. &$$variable is crazy.... is there any work around to it? Maybe this isn't the fault of the library but the way the objects were referenced? – SlackO Sep 18 '20 at 02:05
  • I'm thinking there must be another way to write an assignment/object reference using slightly different notation? – SlackO Sep 18 '20 at 12:41
  • I think a clue to the issue can be found here under "Variable handling": https://www.php.net/manual/en/migration70.incompatible.php – SlackO Sep 18 '20 at 12:43

1 Answers1

2

Here is the solution to this.

The way PHP7 parses double variable references is now different, according to this page:

https://www.php.net/manual/en/migration70.incompatible.php

So the proper re-writing of this:

&$$parent_array[$mod_parent]->addItem(new XNode($mod_name,$mod_url,"Images/doc.gif",false));

is:

&${$parent_array[$mod_parent]}->addItem(new XNode($mod_name,$mod_url,"Images/doc.gif",false));

Likewise a reference such as this in PHP5:

$$var['key'];

Behaves differently under PHP7 and must be hard-noted as this:

${$var['key']};
SlackO
  • 41
  • 3