Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to execute this code (it was working on php5, now I'am on php7):

$this->links->$data[$te]['attributes']['ID'] = $data[$te]['attributes']['URL'];

But I get this error:

ContextErrorException: Notice: Array to string conversion

Thanks in advance

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
421 views
Welcome To Ask or Share your Answers For Others

1 Answer

This is down to the change in how complex variables are resolved in PHP 5 vs 7. See the section on Changes to variable handling here: http://php.net/manual/en/migration70.incompatible.php

The difference is that the expression:

$this->links->$data[$te]['attributes']['ID']

is evaluated like this in PHP 5:

$this->links->{$data[$te]['attributes']['ID']}

and like this in PHP 7:

($this->links->$data)[$te]['attributes']['ID']

See https://3v4l.org/gB0rQ for a cut-down example.

You'll need to amend your code to be explicit, either by using {} as appropriate, or by breaking it down into two lines. In this case, where you've got code that works fine in PHP 5, pick the former, since it will mean the behaviour stays consistent in all versions of PHP.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...