SG 全稱 Superglobals,它的誕生爲了方便快捷操做PHP預約義的超全局變量,用戶定義的超全局變量。php
若是在非CLI模式,SG默認託管PHP預約義的超全局變量, 包括$_GET, $_POST, $_COOOKIE, $_SERVER, $_FILES。git
使用SG類,能讓複雜的代碼變得簡單,開啓自動過濾還能減小代碼量,進而提升咱們的開發效率。github
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複製代碼
重啓php-fpm,就安裝成功啦。數組
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)複製代碼
sg.enable = On/Off
sg.auto_trim = On/Off ; Strip whitespace with PHP trim複製代碼
OLD GET METHOD (Short) | NEW GET METHOD |
---|---|
$_GET['key'] | sg::get('g.key') |
$_POST['key'] | sg::get('p.key') |
$_COOKIE['key'] | sg::get('c.key') |
$_SERVER['key'] | sg::get('s.key') |
$_FILES['key'] | sg::get('f.key') |
OLD GET METHOD (Long) | NEW GET METHOD |
---|---|
$_GET['key']['key1']['key2'] | sg::get('g.key.key1.key2') |
$_POST['key']['key1']['key2'] | sg::get('p.key.key1.key2') |
$_COOKIE['key']['key1']['key2'] | sg::get('c.key.key1.key2') |
$_SERVER['key']['key1']['key2'] | sg::get('s.key.key1.key2') |
$_FILES['key']['key1']['key2'] | sg::get('f.key.key1.key2') |
OLD GET METHOD (isset + trim) | NEW GET METHOD |
---|---|
$key = isset($_GET['key']) ? trim($_GET['key']) : null; | $key = sg::get('g.key'); |
$key = isset($_POST['key']) ? trim($_POST['key']) : null; | $key = sg::get('p.key'); |
$key = isset($_COOKIE['key']) ? trim($_COOKIE['key']) : null; | $key = sg::get('c.key'); |
$key = isset($_SERVER['key']) ? trim($_SERVER['key']) : null; | $key = sg::get('s.key'); |
$key = isset($_FILES['key']) ? trim($_FILES['key']) : null; | $key = sg::get('f.key'); |
(PHP7) 傳統的獲取方式 (??) | 新獲取方式 |
---|---|
$key = $_GET['key'] ?? null; $key = trim($key); | $key = sg::get('g.key'); |
$key = $_POST['key'] ?? null; $key = trim($key); | $key = sg::get('p.key'); |
$key = $_COOKIE['key'] ?? null; $key = trim($key); | $key = sg::get('c.key'); |
$key = $_SERVER['key'] ?? null; $key = trim($key); | $key = sg::get('s.key'); |
$key = $_FILES['key'] ?? null; $key = trim($key); | $key = sg::get('f.key'); |
經過以上的整理,能夠得出一個結論,傳統取值方式容易出錯,若是數組維度越深,代碼複雜度會直線上升。bash
使用SG,這些狀況都變得很簡單。更新、刪除方式類同。app
<?php
var_dump(sg::set('test', 'test apple'));
var_dump(sg::set('user.0.0', 'user 0 apple'));
var_dump(sg::set('user.0.1', 'user 1 apple'));
var_dump(sg::set('user.a.a', 'user a apple'));
var_dump(sg::set('user.a.b', 'user b apple'));複製代碼
以上例子輸出的結果:php-fpm
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
複製代碼
<?php
var_dump(sg::get('test', 'test apple'));
var_dump(sg::get('user');
var_dump(sg::get('not_found', 'def');
var_dump(sg::get('user.1.2.3.4'));複製代碼
以上例子輸出的結果:ui
string(10) "test apple"
array(2) {
[0]=>
array(2) {
[0]=>
string(12) "user 0 apple"
[1]=>
string(12) "user 1 apple"
}
["a"]=>
array(2) {
["a"]=>
string(12) "user a apple"
["b"]=>
string(12) "user b apple"
}
}
string(3) "def"
NULL
複製代碼
<?php
var_dump(sg::has('test'));
var_dump(sg::has('not_found'));複製代碼
以上例子輸出的結果:spa
bool(true)
bool(false)
複製代碼
<?php
var_dump(sg::del('test'));
var_dump(sg::del('user.0.1'));
var_dump(sg::get('user');複製代碼
以上例子輸出的結果:.net
bool(true)
bool(true)
array(2) {
[0]=>
array(1) {
[0]=>
string(12) "user 0 apple"
}
["a"]=>
array(2) {
["a"]=>
string(12) "user a apple"
["b"]=>
string(12) "user b apple"
}
}
複製代碼
演示自動過濾先後空格的例子, 兩種方式開啓自動過濾。
<?php
// sg.auto_trim = On ; php.ini
ini_set('sg.auto_trim', 1);
function One() {
var_dump(sg::set('test', ' test apple ')); // Auto-call PHP trim
}
function Two() {
var_dump(sg::get('test'));
}複製代碼
以上例子輸出的結果:
bool(true)
string(10) "test apple"
複製代碼
SG is open source software under the PHP License v3.01