Can't access Super Globals inside __callStatic?

The following code does not work when installing PHP 5.3.6-13ubuntu3.2 , which makes me wonder why I cannot access $ _SERVER Super Global inside this method.

 <?php header('Content-Type: text/plain'); $method = '_SERVER'; var_dump($$method); // Works fine class i { public static function __callStatic($method, $args) { $method = '_SERVER'; var_dump($$method); // Notice: Undefined variable: _SERVER } } i::method(); 

Does anyone know what's wrong here?

+4
source share
2 answers

As indicated in the manual:

 Note: Variable variables Superglobals cannot be used as variable variables inside functions or class methods. 

( link )

+8
source

[edit - added a possible workaround]

 header('Content-Type: text/plain'); class i { public static function __callStatic( $method, $args) { switch( $method ) { case 'GLOBALS': $var =& $GLOBALS; break; case '_SERVER': $var =& $_SERVER; break; case '_GET': $var =& $_GET; break; // ... default: throw new Exception( 'Undefined variable.' ); } var_dump( $var ); } } i::_SERVER(); i::_GET(); 

[original answer] This is strange. I agree that this could be a PHP error. However, superglobal does work, not as a variable.

 <?php header('Content-Type: text/plain'); $method = '_SERVER'; var_dump($$method); // Works fine class i { public static function __callStatic( $method, $args) { var_dump( $_SERVER ); // works var_dump( $$method ); // Notice: Undefined variable: _SERVER } } i::_SERVER(); 
+2
source

Source: https://habr.com/ru/post/1384974/


All Articles