The following code fails on my install of PHP 5.3.6-13ubuntu3.2
which makes me wonder why I can't access the $_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();
Anyone know what is wrong here?
As indicated in the manual:
Note: Variable variables
Superglobals cannot be used as variable variables inside functions or class methods.
(reference)
[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 weird. I agree that it may be a PHP bug. However, the superglobal does work, just not as a variable 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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With