0

When I switch from PHP 5.6 to 7.0 or 7.2., this statement does not work anymore:

$translator = new stdClass();

$sql = "SELECT name, value FROM ".$tab_translator." WHERE lang_id=:lang_id";
try {
    $fetchTextTranslated = $conn->prepare($sql);
    $fetchTextTranslated->bindValue(':lang_id', (int) trim($translator_lang_id), PDO::PARAM_INT);
    $fetchTextTranslated->execute();
    }
catch(PDOException $e) {
if ($config->debug==1) { echo 'Error: ' . $e->getMessage(); }}

while ($textTranslated = $fetchTextTranslated->fetch(PDO::FETCH_ASSOC)) {
   $translator->$textTranslated['name']=$textTranslated['value'];
}

When I echo $textTranslated['name'] or $textTranslated['value'] I do get data from the table. But I want fetched-data to be in the form of stdClass object $translator and this does not work anymore in PHP 7 and higher.

I would appreciate your help.

Sven
  • 1
  • 1
  • Do you get anything in your error logs? – user3783243 Sep 30 '18 at 01:29
  • Maybe try `$translator->{$textTranslated['name']}` and `$translator->{$textTranslated['value']}`? – qooplmao Sep 30 '18 at 01:30
  • @qooplmao: Hey, it works with {$textTranslated['name']} ! Why is it necessary to set it in curly brackets now? – Sven Sep 30 '18 at 01:34
  • Old PHP: `$translator->{$textTranslated['name']}`. New PHP: `{$translator->$textTranslated}['name']`. Or something like that – ventaquil Sep 30 '18 at 01:37
  • https://www.engineyard.com/blog/what-to-expect-php-7 – Sven Sep 30 '18 at 01:47
  • @Sven. Without the curly braces PHP has to guess what your intent is and, as you see in that page you linked to, their guessing process changed in PHP 7. By adding the curly braces yourself you get rid of that ambiguity and should get a more consistent experience across versions. – qooplmao Sep 30 '18 at 09:35

1 Answers1

0

I'd just like to mention that the use of variable variables is generally a symptom of suboptimal data storage architecture, but there may also be some fringe cases where an advantage may be gained. Generally though, try to avoid variable variables whenever possible.

Please read about Uniform Variable Syntax.

https://www.oreilly.com/ideas/upgrading-to-php7#uniform_variable_syntax

You must wrap your dynamic property in curly braces.

$translator->{$textTranslated['name']} = $textTranslated['value'];
//           ^-----------------------^

This clears up potential confusion/inconsistency when trying to evaluate the line. Again, see my linked document.


I mean, your code could intend to do something entirely different like:

You want to store data as the element with the key name in the $translator->$textTranslated property (a variable property). ...you don't, but I'm just saying, this php7 improvement removes ambiguity while reading left-to-right.

For the record, here is the syntax for the alternative (which you shouldn't use for your task):

 ($translator->$textTranslated)['name'] = $textTranslated['value'];

See the difference?


Additional references:

mickmackusa
  • 43,625
  • 12
  • 83
  • 136