Skip to content
Advertisement

PHP, $this->{$var} — what does that mean?

I have encountered the need to access/change a variable as such:

JavaScript

The context is with CI datamapper get rules. I can’t seem to find what this syntax actually does. What do the {‘s do in this context?

Why can’t you just use:

JavaScript

Advertisement

Answer

This is a variable variable, such that you will end up with $this->{value-of-$val}.

See: http://php.net/manual/en/language.variables.variable.php

So for example:

JavaScript

Working example: http://3v4l.org/QNds9

This of course is working within a class context. You can use variable variables in a local context just as easily like this:

JavaScript

Working example: http://3v4l.org/n16sk

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement