ä¸ć–‡ć–‡ćˇŁ | English Document
SG Full name Superglobals, References all variables available in global scope, SG has extended a new way to manage PHP superglobals variables, Make the management of PHP superglobals variables simple and unified.
These superglobal variables managed by SG are: $_SERVER, $_GET, $_POST, $_FILES, $_COOKIE, $_SESSION, $_REQUEST, $_ENV.
The version v3.0.1 support read raw data from the request body. In the case of POST requests.
Very important point: it is very simple.
- Simple, Fast, Lightweight
- Access PHP superglobals zero-copy, Synchronously update PHP Superglobals variables
- Support custom call function name, Default call PHP trim
- Solve the problem of undefined series when using PHP Superglobals variables (Undefined variable, Undefined offset)
- Use static function method, Replace the PHP array dimension with a decimal point
- Use global statement, Replace the PHP array dimension with a underline
- Support for global $variable the option configuration, Default level one lookup
- Support for read raw data from the POST requests
- PHP 5.4 +
- PHP 7.0 +
git clone https://github.com/yulonghu/sg.git
$ /path/to/php/bin/phpize
$ ./configure --with-php-config=/path/to/php/bin/php-config
$ make && make install
extension=sg.so
[sg]
sg.enable = On
Restart the php-fpm.
global $g_key, $p_key, $c_key, $s_key, $f_key, $n_key, $e_key, $r_key
array sg::all(void)
mixed sg::get(string $key [, mixed $default_value = null])
bool sg::set(string $key, mixed $value)
bool sg::has(string $key)
bool sg::del(string $key [, mixed $... ])
mixed sg::getRaw([mixed $default_value = null [, int $maxlen]])
mixed sg::getCache(string $key [, mixed $default_value = null])
Options | Permission | Type | Default | Desc |
---|---|---|---|---|
sg.enable | PHP_INI_SYSTEM | bool | 0 | 0 Trun-Off 1 Turn-On |
sg.global_level | PHP_INI_SYSTEM | bool | 1 | 1 Limit Level 0 Unlimited Level |
sg.func_name | PHP_INI_ALL | char | trim | PHP trim, Support for custom function |
- When management PHP sessions (MapKey = n), First call the function session_start()
PHP Predefined Superglobals | SG Key | global statement | Method Example |
---|---|---|---|
$GLOBALS | - | - | sg::all() |
$_SERVER | s | global $s | sg::get/set/has/del('s') |
$_GET | g | global $g | sg::get/set/has/del('g') |
$_POST | p | global $p | sg::get/set/has/del('p') |
$_FILES | f | global $f | sg::get/set/has/del('f') |
$_COOKIE | c | global $c | sg::get/set/has/del('c') |
$_SESSION | n | global $n | sg::get/set/has/del('n') |
$_REQUEST | r | global $r | sg::get/set/has/del('r') |
$_ENV | e | global $e | sg::get/set/has/del('e') |
<?php
$_GET['key'] = 'GET_test_key';
function testGlobal()
{
global $g_key;
var_dump($g_key);
$g_key = 'NEW_GET_test_key';
}
testGlobal();
var_dump(sg::get('g.key'));
var_dump($GLOBALS['g_key']);
var_dump($g_key);
var_dump($_GET['key']);
The above example will output:
string(12) "GET_test_key"
string(16) "NEW_GET_test_key"
string(16) "NEW_GET_test_key"
string(16) "NEW_GET_test_key"
string(16) "NEW_GET_test_key"
<?php
$_GET['key']['key1']['key2'] = 'GET_test_key';
function testGlobal()
{
global $g_key_key1_key2;
}
testGlobal();
var_dump(sg::get('g.key.key1.key2'));
var_dump($GLOBALS['g_key_key1_key2']);
var_dump($g_key_key1_key2);
var_dump($_GET['key']['key1']['key2']);
The above example will output:
string(12) "GET_test_key"
string(12) "GET_test_key"
string(12) "GET_test_key"
string(12) "GET_test_key"
<?php
ini_set('sg.func_name', 'decryptTest');
$_POST['key'] = 'IEEgQmFuYW5hIA==';
function decryptTest($data)
{
return trim(base64_decode($data));
}
global $p_key;
var_dump($p_key);
The above example will output:
string(8) "A Banana"
<?php
$key = 'test';
$val = 'A Banana';
echo "------------------start\n";
var_dump(sg::get($key));
var_dump(sg::get($key, 'def'));
var_dump(sg::has($key));
echo "------------------set\n";
var_dump(sg::set($key, $val));
echo "------------------get\n";
var_dump(sg::get($key));
var_dump(sg::get($key, 'def'));
var_dump(sg::has($key));
echo "------------------del\n";
var_dump(sg::del($key));
echo "------------------get\n";
var_dump(sg::get($key));
var_dump(sg::has($key));
The above example will output:
------------------start
NULL
string(3) "def"
bool(false)
------------------set
bool(true)
------------------get
string(8) "A banana"
string(8) "A banana"
bool(true)
------------------del
bool(true)
------------------get
NULL
bool(false)
Same as $GLOBALS
<?php
ini_set('sg.func_name', 'decryptTest');
function decryptTest($data)
{
return trim(base64_decode($data));
}
function encryptTest($data)
{
return base64_encode(trim($data));
}
sg::set('user', encryptTest(' A Banana '));
var_dump(sg::get('user'));
The above example will output:
string(8) "A Banana"
SG is open source software under the PHP License v3.01