Skip to content

Instantly share code, notes, and snippets.

@bulton-fr
Created May 15, 2016 10:27
Show Gist options
  • Save bulton-fr/dec522d28bc1c554784deb41681e28d4 to your computer and use it in GitHub Desktop.
Save bulton-fr/dec522d28bc1c554784deb41681e28d4 to your computer and use it in GitHub Desktop.
Test to create var in global scope from class method
<?php
/**
* Test with https://3v4l.org/ZWMBt#v500
*
* $test3 is created for 5.0.0 - 5.6.21, hhvm-3.9.1 - 3.12.0, 7.0.0 - 7.0.6
*/
$test1 = 'test1';
var_dump($GLOBALS);
/**
array (size=9)
'_GET' => array (size=0) empty
'_POST' => array (size=0) empty
'_COOKIE' => array (size=0) empty
'_FILES' => array (size=0) empty
'_ENV' => array (size=0) empty
'_REQUEST' => array (size=0) empty
'_SERVER' => array (size=32) [...]
'GLOBALS' => &array
'test1' => string 'test1' (length=5)
*/
class Foo
{
public static function bar()
{
//$test3 = 'test3';
$GLOBALS['test3'] = 'test3';
var_dump($GLOBALS);
}
}
$test2 = 'test2';
Foo::bar();
/**
array (size=9)
'_GET' => array (size=0) empty
'_POST' => array (size=0) empty
'_COOKIE' => array (size=0) empty
'_FILES' => array (size=0) empty
'_ENV' => array (size=0) empty
'_REQUEST' => array (size=0) empty
'_SERVER' => array (size=32) [...]
'GLOBALS' => &array
'test1' => string 'test1' (length=5)
'test2' => string 'test2' (length=5)
'test3' => string 'test3' (length=5)
*/
var_dump(isset($test3)); //boolean true
var_dump($test3); //string 'test3' (length=5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment