Skip to content

Instantly share code, notes, and snippets.

@darraghenright
Created February 25, 2016 15:35
Show Gist options
  • Save darraghenright/af53f6c800dd3a9279a3 to your computer and use it in GitHub Desktop.
Save darraghenright/af53f6c800dd3a9279a3 to your computer and use it in GitHub Desktop.

Question

You are using PHP7.

Firstly, note that $nodes in the statement $result = $node->$nodes[$i] = $value is a variable variable. The problematic part of the statement is $node->$nodes[$i] itself.

As specified in the PHP7 documention for Backward Incompatible Changes, a number of changes have been made to variable handling, including Changes to the handling of indirect variables, properties, and methods.

This states that:

Indirect access to variables, properties, and methods will now be evaluated strictly in left-to-right order, as opposed to the previous mix of special cases. The table below shows how the order of evaluation has changed.

The documentation provides a few examples where expected behaviour has changed. So what has changed in this case?

  • In previous versions, $node->$nodes[$i] is evaluated as $node->{$nodes[$i]}.
  • In PHP7, $node->$nodes[$i] is evaluated as ($node->$nodes)[$i].

To illustrate with a quick example:

<?php

// create a class
$node = new stdClass();

// add some public properties
$node->foo = 'I am foo!';
$node->bar = 'I am bar!';

// declare an array of `node` names
$nodes = ['foo', 'bar'];

$i = 0;
echo $node->$nodes[$i];

Working example showing behaviour in different versions of PHP.

Prior to PHP7, $nodes[$i] is evaulated first which is is $nodes[0]. This yields the string foo. Then, thanks to some "variable variable" magic, this is evaluated as $nodes->foo. Which, of course, is an existing property with a string value I am foo!.

IN PHP7, because of the documented changes in evaluation, $node->$nodes is evaluated first. What is $nodes? It's an array, not a string, so cannot be used to dereference a property. Hence the displayed Array to string notice.

This appears to be a known issue in Joomla. It looks like some work has gone into resolving this.

Your options here are to:

  1. Use a previous version of PHP.
  2. Explore if there is a more recent version of Joomla to which you can upgrade that has fixed this issue.

Hope this helps :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment