Search code examples
phpbraces

{$_GET['action']}(); What does mean all these braces?


Could someone please help to understand this syntaxe trick in php:

enter $controller->{$_GET['action']}();

I'm talking about the

{$_GET['action']}();

I'm trying to understand the mvc pattern on this blog http://r.je/mvc-in-php.html but it's realy help my if I could understand this syntax trick...

Thanks


Solution

  • It's a way to dynamically access a member of an object.

    $key = 'test';
    $object->{$key}
    

    Is equivalent to:

    $object->test
    

    In your example, someone is running whatever method specified by $_GET['action'] (which is the action variable in the querystring) on the $controller.

    See variable variables and variable functions in the manual.