PHP SG擴展管理Superglobals

A Simple PHP Superglobals Managementphp

GitHub: github.com/yulonghu/sg
git

Introduction

SG is the same as PHP Superglobals, Management global variables are $_GET, $_POST, $_COOOKIE, $_SERVER, $_FILES, User-defined global variables. Use SG can save a lot of PHP code. github

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

Requirements

  • PHP 7.0 +

DownLoad

git clone https://github.com/yulonghu/sg.git
複製代碼

Compile SG in Linux

$ /path/to/php7/bin/phpize
$ ./configure --with-php-config=/path/to/php7/bin/php-config
$ make && make install
複製代碼

Add the follow information to your php.ini

extension=sg.so

[sg]
sg.enable = On複製代碼

Restart the web serverweb

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

PHP Superglobals

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')

User-defined global variables

sg::set

<?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 above example will output:bash

bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
複製代碼

sg::get

<?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:php7

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
複製代碼

sg::has

<?php
var_dump(sg::has('test'));
var_dump(sg::has('not_found'));複製代碼

The above example will output:app

bool(true)
bool(false)
複製代碼

sg::del

<?php
var_dump(sg::del('test'));
var_dump(sg::del('user.0.1'));
var_dump(sg::get('user');複製代碼

The above example will output:ui

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"
  }
}
複製代碼

License

SG is open source software under the PHP License v3.01spa

相關文章
相關標籤/搜索