Why does PHP accept instantiating a class through a value of a variable, but not a vector?

0

Why does PHP accept that I create a class through a variable like this:

$MeuController = 'posts';
$controller = new \app\controllers\admin\$MeuController();

But not so:

$MeuController[0] = 'posts';
$controller = new \app\controllers\admin\$MeuController[0]();

The real case is that I get the routes through an array in an MVC project, and when I try to create that way I can not, I have to do this:

$MeuController = $ArrayController[0];

Am I doing something wrong or is it a limitation?

    
asked by anonymous 12.12.2015 / 02:07

1 answer

1

I'm not sure, but if this does not work it's probably to avoid complex variables, this can make the php interpreter difficult, so much so that in php7 some things got more limited, as php7 reference

Uniform variable syntax

This change brings much greater "orthogonality" to the variable operators in PHP. It allows for a number of new combinations of operators that were previously not allowed and thus introduces new ways to achieve certain operations.

// nesting ::
$foo::$bar::$baz // Acessa a propriedade $baz de $foo::$bar

// nesting ()
foo()() // Executa o retorno de foo()

// Operadores em expressões incluidas semelhantes ao javascript
(function () {})() // Sintaxe IIFE do JS

The ability to arbitrarily combine variable operators came from reverting the evaluation semantics of the indirect variable, property, and method references. The new behavior is more intuitive and always follows a left-to-right evaluation order:

                        // maneira antiga         // nova maneira
$$foo['bar']['baz']     ${$foo['bar']['baz']}     ($$foo)['bar']['baz']
$foo->$bar['baz']       $foo->{$bar['baz']}       ($foo->$bar)['baz']
$foo->$bar['baz']()     $foo->{$bar['baz']}()     ($foo->$bar)['baz']()
Foo::$bar['baz']()      Foo::{$bar['baz']}()      (Foo::$bar)['baz']()

Note that the "new way" works in older versions.

    
12.12.2015 / 02:32