This is an utility I made to access to common global variables in a mode I prefer: $_GET -> foo against $_GET['foo'].
Where's the utility?
- you can use this function for other variables too
- backward compatibility: array access is allowed too, so
$_GET['foo']is accepted - integer and float values will be converted from string to their respective values
- usage on inclusion
- prevent not defined cells errors
When you include this script it will automatically conver arrays in $GLOBALS.
If a variable is not defined you don't have to catch errors because undefined cells will return null.
<?php
// URL is http://hostname/path?lorem=ipsum
$function = require_once 'double_access.php';
var_dump(
$_GET,
$_GET -> lorem,
$_GET['lorem'],
$_GET['undefined']
);
// object(ArrayObject)[3]
// public 'lorem' => string 'ipsum' (length=5)
// string 'ipsum' (length=5)
// string 'ipsum' (length=5)
// null
?><?php
$function = require_once 'double_access.php';
$tmp = array(
'foo' => array(
0 => 'bar',
1 => 5,
'test' => 6.42,
),
);
$function($tmp);
var_dump(
$tmp['foo'],
$tmp -> foo,
$tmp['foo'][0],
$tmp -> foo[0],
$tmp -> foo -> test
);
// object(ArrayObject)[1]
// string 'bar' (length=3)
// int 5
// public 'test' => float 6.42
// object(ArrayObject)[1]
// string 'bar' (length=3)
// int 5
// public 'test' => float 6.42
// string 'bar' (length=3)
// string 'bar' (length=3)
// float 6.42
?>