1
- # SG - PHP Superglobals Management
1
+ # SG - PHP Syntax Sugar Extension
2
+
2
3
[ ![ Build Status] ( https://travis-ci.org/yulonghu/sg.svg?branch=master )] ( https://travis-ci.org/yulonghu/sg )
3
4
4
- A Simple PHP Superglobals Management
5
+ A simple PHP syntax sugar extension
5
6
6
7
[ 中文文档] ( https://github.com/yulonghu/sg/blob/master/README_ZH.md )
7
8
8
9
### Introduction
9
10
10
11
[ SG] ( https://github.com/yulonghu/sg ) Full name [ Superglobals] ( http://php.net/manual/en/language.variables.superglobals.php ) , 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.
11
12
12
- These superglobal variables managed by SG are: $GLOBALS, $_ SERVER, $_ GET, $_ POST, $_ FILES, $_ COOKIE, $_ SESSION, $_ REQUEST, $_ ENV.
13
+ These superglobal variables managed by SG are: $_ SERVER, $_ GET, $_ POST, $_ FILES, $_ COOKIE, $_ SESSION, $_ REQUEST, $_ ENV.
14
+
15
+ Very important point: it is very simple.
13
16
14
17
### Features
15
18
- Simple, Fast, Lightweight
16
19
- Access PHP superglobals zero-copy, Synchronously update PHP Superglobals variables
17
- - Support string values automatically filters spaces [ PHP trim] ( http://php.net/manual/en/function.trim.php )
20
+ - Support for custom function, Default call [ PHP trim] ( http://php.net/manual/en/function.trim.php )
18
21
- Solve the problem of undefined series when using PHP Superglobals variables (Undefined variable, Undefined offset)
19
- - Replace the PHP array dimension with a decimal point
22
+ - Use static function method, Replace the PHP array dimension with a decimal point
23
+ - Use global statement, Replace the PHP array dimension with a underline
24
+ - Support for global key the option configuration, Default level one lookup
20
25
21
26
## Install
22
27
### Supported Version
@@ -56,26 +61,27 @@ bool sg::has(string $key)
56
61
bool sg::del(string $key [, mixed $... ])
57
62
```
58
63
59
- ### Inis
60
- ``` ini
61
- sg.enable = On/Off ; Default Off
62
- sg.auto_trim = On/Off ; Strip whitespace with PHP trim
63
- ```
64
+ ### Inis(php.ini)
65
+ | Options | Permission | Type | Default | Desc |
66
+ | ---| ---| ---| ---| ---|
67
+ | sg.enable| PHP_INI_SYSTEM| bool| 0| 0 Trun-Off 1 Turn-On|
68
+ | sg.global_level| PHP_INI_SYSTEM| bool| 1| 1 Limit Level 0 Unlimited Level|
69
+ | sg.func_name| PHP_INI_ALL| char| trim| [ PHP trim] ( http://php.net/manual/en/function.trim.php ) , Support for custom function|
64
70
65
- ### Superglobals <=> SG HashMap
71
+ ### Hash Map
66
72
- When management PHP sessions (MapKey = n), First call the function session_start()
67
73
68
- | PHP Predefined Superglobals| SG MapKey | Method Example|
69
- | ------ | ------ | ------ |
70
- | $GLOBALS| -| sg::all()|
71
- | $_ SERVER| s| sg::get/set/has/del('s')|
72
- | $_ GET| g| sg::get/set/has/del('g')|
73
- | $_ POST| p| sg::get/set/has/del('p')|
74
- | $_ FILES| f| sg::get/set/has/del('f')|
75
- | $_ COOKIE| c| sg::get/set/has/del('c')|
76
- | $_ SESSION| n| sg::get/set/has/del('n')|
77
- | $_ REQUEST| r| sg::get/set/has/del('r')|
78
- | $_ ENV| e| sg::get/set/has/del('e')|
74
+ | PHP Predefined Superglobals| SG Key | global statement | Method Example|
75
+ | ------ | ------ | ------ | ------ |
76
+ | $GLOBALS| -| - | sg::all()|
77
+ | $_ SERVER| s| global $s | sg::get/set/has/del('s')|
78
+ | $_ GET| g| global $g | sg::get/set/has/del('g')|
79
+ | $_ POST| p| global $p | sg::get/set/has/del('p')|
80
+ | $_ FILES| f| global $f | sg::get/set/has/del('f')|
81
+ | $_ COOKIE| c| global $c | sg::get/set/has/del('c')|
82
+ | $_ SESSION| n| global $n | sg::get/set/has/del('n')|
83
+ | $_ REQUEST| r| global $r | sg::get/set/has/del('r')|
84
+ | $_ ENV| e| global $e | sg::get/set/has/del('e')|
79
85
80
86
### Usage
81
87
@@ -88,6 +94,7 @@ var_dump(sg::set('user.0.1', 'user 1 apple')); // Same as $GLOBALS['test'][0][1]
88
94
var_dump(sg::set('user.a.a', 'user a apple')); // Same as $GLOBALS['test']['a']'a']
89
95
var_dump(sg::set('user.a.b', 'user b apple')); // Same as $GLOBALS['test']['a']'b']
90
96
```
97
+
91
98
The above example will output:
92
99
``` txt
93
100
bool(true)
@@ -170,74 +177,6 @@ array(2) {
170
177
### array sg::all(void)
171
178
The same result as [ $GLOBALS] ( https://www.php.net/manual/zh/reserved.variables.globals.php )
172
179
173
- ## Example
174
-
175
- ### Example #1
176
-
177
- | OLD GET METHOD (Short)| NEW GET METHOD|
178
- | ------ | ------ |
179
- | $_ SERVER[ 'key'] | sg::get('s.key')|
180
- | $_ GET[ 'key'] | sg::get('g.key')|
181
- | $_ POST[ 'key'] | sg::get('p.key')|
182
- | $_ FILES[ 'key'] | sg::get('f.key')|
183
- | $_ COOKIE[ 'key'] | sg::get('c.key')|
184
- | $_ SESSION[ 'key'] | sg::get('n.key')|
185
- | $_ REQUEST[ 'key'] | sg::get('r.key')|
186
- | $_ ENV[ 'key'] | sg::get('e.key')|
187
-
188
- | OLD GET METHOD (Long)| NEW GET METHOD|
189
- | ------ | ------ |
190
- | $_ SERVER[ 'key'] [ 'key1' ] [ 'key2'] | sg::get('s.key.key1.key2')|
191
- | $_ GET[ 'key'] [ 'key1' ] [ 'key2'] | sg::get('g.key.key1.key2')|
192
- | $_ POST[ 'key'] [ 'key1' ] [ 'key2'] | sg::get('p.key.key1.key2')|
193
- | $_ FILES[ 'key'] [ 'key1' ] [ 'key2'] | sg::get('f.key.key1.key2')|
194
- | $_ COOKIE[ 'key'] [ 'key1' ] [ 'key2'] | sg::get('c.key.key1.key2')|
195
- | $_ SESSION[ 'key'] [ 'key1' ] [ 'key2'] | sg::get('n.key.key1.key2')|
196
- | $_ REQUEST[ 'key'] [ 'key1' ] [ 'key2'] | sg::get('r.key.key1.key2')|
197
- | $_ ENV[ 'key'] [ 'key1' ] [ 'key2'] | sg::get('e.key.key1.key2')|
198
-
199
- | OLD GET METHOD (isset + trim)| NEW GET METHOD|
200
- | ------ | ------ |
201
- | $key = isset($_ SERVER[ 'key'] ) ? trim($_ SERVER[ 'key'] ) : null;| $key = sg::get('s.key');|
202
- | $key = isset($_ GET[ 'key'] ) ? trim($_ GET[ 'key'] ) : null;| $key = sg::get('g.key');|
203
- | $key = isset($_ POST[ 'key'] ) ? trim($_ POST[ 'key'] ) : null;| $key = sg::get('p.key');|
204
- | $key = isset($_ FILES[ 'key'] ) ? trim($_ FILES[ 'key'] ) : null;| $key = sg::get('f.key');|
205
- | $key = isset($_ COOKIE[ 'key'] ) ? trim($_ COOKIE[ 'key'] ) : null;| $key = sg::get('c.key');|
206
- | $key = isset($_ SESSION[ 'key'] ) ? trim($_ SESSION[ 'key'] ) : null;| $key = sg::get('n.key');|
207
- | $key = isset($_ REQUEST[ 'key'] ) ? trim($_ REQUEST[ 'key'] ) : null;| $key = sg::get('r.key');|
208
- | $key = isset($_ ENV[ 'key'] ) ? trim($_ ENV[ 'key'] ) : null;| $key = sg::get('e.key');|
209
-
210
- | (PHP7) OLD GET METHOD (??)| NEW GET METHOD|
211
- | ------ | ------ |
212
- | $key = $_ SERVER[ 'key'] ?? null; $key = trim($key);| $key = sg::get('s.key');|
213
- | $key = $_ GET[ 'key'] ?? null; $key = trim($key);| $key = sg::get('g.key');|
214
- | $key = $_ POST[ 'key'] ?? null; $key = trim($key);| $key = sg::get('p.key');|
215
- | $key = $_ FILES[ 'key'] ?? null; $key = trim($key);| $key = sg::get('f.key');|
216
- | $key = $_ COOKIE[ 'key'] ?? null; $key = trim($key);| $key = sg::get('c.key');|
217
- | $key = $_ SESSION[ 'key'] ?? null; $key = trim($key);| $key = sg::get('n.key');|
218
- | $key = $_ REQUEST[ 'key'] ?? null; $key = trim($key);| $key = sg::get('r.key');|
219
- | $key = $_ ENV[ 'key'] ?? null; $key = trim($key);| $key = sg::get('e.key');|
220
-
221
- ### sg.auto_trim
222
-
223
- For best performance, sg will change the original value if the trim condition is met. Avoid repeat the trim operation each time you take a value.
224
-
225
- ``` php
226
- <?php
227
- ini_set('sg.auto_trim', 1);
228
- function One() {
229
- var_dump(sg::set('test', ' test apple ')); // Auto-call PHP trim
230
- }
231
- function Two() {
232
- var_dump(sg::get('test'));
233
- }
234
- ```
235
- The above example will output:
236
- ``` txt
237
- bool(true)
238
- string(10) "test apple"
239
- ```
240
-
241
180
## License
242
181
SG is open source software under the [ PHP License v3.01] ( http://www.php.net/license/3_01.txt )
243
182
0 commit comments