Magic Guide Syntactic sugar often provides programmers with a more practical coding method, which can make the code more concise and smooth, and the semantics are more natural. This article introduces the PHP syntactic sugar extension written by the author, which extends a new way of obtaining PHP superglobal variables. The author of this article, Fan Jiapeng, is a member of the 360 Technical Committee--Web Server Sub-TC Committee.
1. Talk about syntactic sugar
First of all, the word "syntactic sugar" is by no means a derogatory term. It can bring convenience to our development work. It is a lightweight and convenient way of writing. It will neither adversely affect the use of the language itself, nor will it There is a loss in performance.
Usually, the use of syntactic sugar can increase the readability of the program, reduce the complexity of the program, and reduce the chance of errors in coding. It is also friendly to development engineers and can improve our development efficiency.
Excellent syntactic sugar should be an injection of soul thinking and the application of simple writing. I use a picture to represent it here:
2. What is SG?
The full name of SG is Superglobals, which refers to all variables available in the global scope. SG extends a new way of obtaining PHP superglobal variables.
These superglobals are: _SERVER, _GET, _POST, _FILES, _COOKIE, _SESSION, _REQUEST, _ENV.
Of course, it can also be applied to custom variable scenarios.
A very important point: it's simple !
2.1 Project Background
The starting point of this idea is very simple, we should be able to guess what it does from the extension name.
Under the premise of optimal performance:
- It can simplify the HTTP parameter acquisition method in an easy-to-understand manner
- It is necessary to perform unified filtering, conversion, and decryption operations on HTTP parameter values.
- Before obtaining HTTP parameters, some Predefined Operations need to be performed
- For all behaviors of HTTP parameters, the corresponding PHP Superglobal needs to be updated synchronously
- It is used only when declared, instead of sweeping PHP Superglobals at the beginning of the request
- In the global syntax, extend the ability to obtain HTTP parameters
So, SG appeared to solve the above problems, it provides a sweeter syntax, and v3.0.0 has been released.
2.2 Project address
https://github.com/yulonghu/sg
Welcome to submit Issues~
The currently supported PHP versions, as shown in the following figure:
3. Characteristics of SG
- Simple, fast, lightweight
- Zero-copy access to PHP super-global variables, using SG will update PHP super-global variables synchronously
- Supports calling a custom function before taking a value. By default, a string variable will automatically call PHP trim
- Solve the problem of undefined series when using PHP super global variables (Undefined variable, Undefinedoffset)
- Replace PHP array dimensions with decimal points when using static methods
- When using the global declaration method, replace the PHP array dimension with an underscore
- Support configurable global $variable search depth, default one level search
4. Configuration items (php.ini)
configuration item | permission | Types of | Defaults | illustrate |
---|---|---|---|---|
sg.enable | PHP_INI_SYSTEM | bool | 0 | 0 off 1 on |
sg.global_level | PHP_INI_SYSTEM | bool | 1 | 1 only supports first level search 0 unlimited search |
sg.func_name | PHP_INI_ALL | char | trim | The PHP trim function is called by default , and custom functions are also supported |
5、Hash Map
PHP superglobals | SG key (keyword abbreviation) | global declaration | function |
---|---|---|---|
$GLOBALS | without | without | 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 | aggregate $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') |
6. Flowchart
6.1 global declaration method (PHP7)
6.2 Function method
7、API
7.1 global declaration method
global $g_key, $p_key, $c_key, $s_key, $f_key, $n_key, $e_key, $r_key
7.2 Static methods
bool sg::set(string $key, mixed $value)
mixed sg::get(string $key [, mixed $default_value = null])
bool sg::has(string $key)
bool sg::del(string $key [, mixed $... ])
array sg::all(void)
8. Examples
8.1 Example of global declaration
8.1.1 sg.global_level = 1
<?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 output of the above example:
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"
8.1.2 sg.global_level = 0
<?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 output of the above example:
string(12) "GET_test_key"
string(12) "GET_test_key"
string(12) "GET_test_key"
string(12) "GET_test_key"
8.1.3 sg.func_name
<?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 output of the above example:
string(8) "A Banana"
8.2 Static method example
8.2.1 get/set/has/del()
<?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 output of the above example:
------------------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)
8.2.2 sg.func_name
<?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 output of the above example:
string(8) "A Banana"
9. Performance test
After talking for a long time, how is the performance? I did a simple ab test (ab -c100 -n10000) in the local environment, the PHP test code is as follows:
9.1 default - source code
<?php
/* default.php */
for($i = 0; $i < 1000; $i++) {
if(isset($_GET['key'])) {
var_dump(trim($_GET['key']));
}
}
9.2 global declaration method - source code
<?php
/* sg.php */
for($i = 0; $i < 1000; $i++) {
global $g_key;
var_dump($g_key);
}
The results of the ab test are as follows:
9.3 default - results
$ ab -c100 -n10000 localhost/default.php?key=hello_world
Concurrency Level: 100
Time taken for tests: 1.615 seconds
Complete requests: 10000
Failed requests: 0
Write errors: 0
Total transferred: 251370000 bytes
HTML transferred: 250000000 bytes
Requests per second: 6190.21 [#/sec] (mean)
Time per request: 16.155 [ms] (mean)
Time per request: 0.162 [ms] (mean, across all concurrent requests)
Transfer rate: 151956.36 [Kbytes/sec] received
9.4 global declaration method - result
$ ab -c100 -n10000 localhost/sg.php?key=hello_world
Concurrency Level: 100
Time taken for tests: 1.441 seconds
Complete requests: 10000
Failed requests: 0
Write errors: 0
Total transferred: 251931544 bytes
HTML transferred: 250557708 bytes
Requests per second: 6938.67 [#/sec] (mean)
Time per request: 14.412 [ms] (mean)
Time per request: 0.144 [ms] (mean, across all concurrent requests)
Transfer rate: 170709.87 [Kbytes/sec] received
10. Summary
SG has the following advantages:
- Compatible with current mainstream PHP versions
- Provides a sweeter syntax that enriches the application of Superglobals
- We always believe that simplicity is king
Note: The global declaration method currently only supports immutable variable names.
(360 technology original content, please keep the QR code at the end of the article for reprinting, thank you~)
About 360 Technology
360 Technology is a technology sharing public account created by the 360 technology team, and pushes dry technical content every day
For more technical information, please pay attention to the WeChat public account of "360 Technology"