1

I've been using PHP for some basics Back-End development for a while now. I saw something about interpretations of variables while I was looking for some changes which came with PHP 7. I'm not using them and it would be great if someone can explain why to use them?

What I mean is: enter image description here

What are the pros of using them?

Dawid Zbiński
  • 5,521
  • 8
  • 43
  • 70
  • 2
    Variable variables (`$$foo`) are almost always a bad idea. Variable property access (`Foo::$bar`) can have its uses. You'll realise that when you encounter a situation where it's beneficial. Listing arbitrary situations in which it can be useful is a bit aimless and broad for an SO question. – deceze Dec 27 '16 at 11:33
  • I wasn't familiar with the term "interpretation". The manual refers to that as [variable variables](http://php.net/manual/en/language.variables.variable.php) and [variable functions](http://php.net/manual/en/functions.variable-functions.php). – Álvaro González Dec 27 '16 at 11:35
  • Alright, I didn't realize that it's that broad. I'm deleting the "when" question and will just stick to the "why" part of it. Thank you. – Dawid Zbiński Dec 27 '16 at 11:35
  • Just found you took that table from the PHP/7 migration guide ([Backward incompatible changes](http://php.net/manual/en/migration70.incompatible.php#migration70.incompatible.variable-handling.indirect)). There isn't any PHP feature called "interpretation", it just means that PHP parser now evaluates certain expressions in a different order. – Álvaro González Dec 27 '16 at 11:39
  • @ÁlvaroGonzález I couldn't find it in documentation as I was looking for the "interpretation" keyword from the image above. I'll probably change the title here. – Dawid Zbiński Dec 27 '16 at 11:39
  • 2
    [Duplicate](http://stackoverflow.com/questions/3523670/whats-an-actual-use-of-variable-variables) – Quentin Dec 27 '16 at 11:46
  • I personnaly like to use them to make my code cleaner and more intuitive when treating forms. See a [demo here](http://phpio.net/s/2e6f). – Amin NAIRI Dec 27 '16 at 14:11
  • @Gradiuss Beware that your example basically recreates [register_globals](http://php.net/register_globals), an infamous PHP feature that made it easy to get your site hacked. You can also use [extract](http://php.net/extract), which can be nearly as bad. – Álvaro González Dec 27 '16 at 15:34
  • Thanks @ÁlvaroGonzález, I'm aware that this can lead to security breach. When I say *I personnaly like to use them*, I mean that this would be for me one of the use case, but in reality I don't because of those security issues. Of course, dynamic variable car most of the time be avoided with a little more code understanding in mind. – Amin NAIRI Dec 27 '16 at 15:54

1 Answers1

1

You use variable interpretation in situations when you need to dynamically reference a variable and don't want to use an array. Generally I would not recommend using it, as you lose benefits such as static code analysis.

Jirka Hrazdil
  • 3,983
  • 1
  • 14
  • 17