GitHub - yulonghu/sg: A Simple PHP Superglobals Management
source link: https://github.com/yulonghu/sg
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
README.md
SG - PHP Superglobals Management
A Simple PHP Superglobals Management
Introduction
SG Full name Superglobals, It is very easy to management PHP predefined Superglobals variables and User-defined Superglobals variables.
If in non CLI mode, SG default registration Superglobals include $_GET, $_POST, $_COOOKIE, $_SERVER, $_FILES.
Use SG can save a lot of PHP code, Improve development efficiency.
Features
- Simple, Fast, Lightweight
- Access global variable zero-copy
- Strip whitespace with PHP trim
- Solve problems with PHP undefined series (Undefined variable, Undefined offset)
Install
Supported Version
- PHP 5.4 +
- PHP 7.0 +
DownLoad
git clone https://github.com/yulonghu/sg.git
Compile SG in Linux
$ /path/to/php/bin/phpize
$ ./configure --with-php-config=/path/to/php/bin/php-config
$ make && make install
Add the follow information to your php.ini
extension=sg.so [sg] sg.enable = On
Restart the php-fpm.
Methods
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)
Inis
sg.enable = On/Off sg.auto_trim = On/Off ; Strip whitespace with PHP trim
Example
Get PHP Predefined Superglobals variable
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');Management User-defined Superglobal variables
bool sg::set(string $key, mixed $value)
<?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')); // The follow methods are not recommended, update PHP predefined superglobal variable values. var_dump(sg::set('g.key', 'value')); var_dump(sg::set('p.key', 'value')); var_dump(sg::set('c.key', 'value')); var_dump(sg::set('s.key', 'value')); var_dump(sg::set('f.key', 'value'));
The above example will output:
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
mixed sg::get(string $key [, mixed $default_value = null])
<?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'));
The above example will output:
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
bool sg::has(string $key)
<?php var_dump(sg::has('test')); var_dump(sg::has('not_found'));
The above example will output:
bool(true)
bool(false)
bool sg::del(string $key)
<?php var_dump(sg::del('test')); var_dump(sg::del('user.0.1')); var_dump(sg::get('user');
The above example will output:
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"
}
}
sg.auto_trim
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.
<?php 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')); }
The above example will output:
bool(true)
string(10) "test apple"
License
SG is open source software under the PHP License v3.01
Recommend
-
43
README.md Asf - API Services Framework
-
168
GitHub is where people build software. More than 28 million people use GitHub to discover, fork, and contribute to over 79 million projects.
-
195
SPX - A simple profiler for PHP Click here for a live demo of the analysis screen SP...
-
206
Files Permalink Latest commit message Co...
-
2
PHP Records This aims to be a bare-bones library for creating immutable records in PHP. Example: use Withinboredom\Record; class Person extends Record { public function __construct(public readonly string $firstN...
-
1
master Go to file Co...
-
4
Deondazy Core PHP MVC Framework Core is a simple light weight PHP MVC Framework, built to use for my PHP projects where Laravel and other PHP frameworks have way too much for my needs. Core has a simple Router, User Authe...
-
147
README.md
-
3
README.md
-
3
vue-path-store PathStore is a simple, lightweight, shared state management solution for Vue. At its heart lays a simple reactive store, which uses the dot notation path syntax for data mutat...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK