1

Background: I have an existing class with static functions that I'd like to reuse for another part of the same project. The static functions use $_GET, etc.

Here is the baffling code. First, the class:

<?php

// ./fakeapi/fakeapi.php

class FakeAPI {

    static public function test (){

        $var = '_GET';
        ${$var}['newVar'] = '123';
        print_r($_GET);

    }
}

And the file requiring it:

<?php

require_once 'fakeapi/fakeapi.php';

FakeAPI::test();

$var = '_GET';
${$var}['newVar'] = '123';
print_r($_GET);

prints:

Array
(
)
Array
(
    [newVar] => 123
)

...and I have no idea why. It's not a scope issue, because if I set $_GET['newVar'] = '123'; in the class function, it works. Does anyone know what's going on here?

keyboardSmasher
  • 2,661
  • 18
  • 20
  • Your question answered here: http://stackoverflow.com/questions/6176269/php-variable-variables-problem?answertab=votes#tab-top –  Jul 21 '13 at 00:38

1 Answers1

0

According to PHP Manual Variable Variables

Warning

Please note that variable variables cannot be used with PHP's Superglobal arrays within functions or class methods. The variable $this is also a special variable that cannot be referenced dynamically.