{"id":15022903,"url":"https://github.com/yulonghu/sg","last_synced_at":"2025-04-09T19:51:01.407Z","repository":{"id":247300285,"uuid":"176443263","full_name":"yulonghu/sg","owner":"yulonghu","description":"A Simple PHP Superglobals Management","archived":false,"fork":false,"pushed_at":"2019-08-22T10:29:38.000Z","size":83,"stargazers_count":124,"open_issues_count":0,"forks_count":3,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-23T21:45:54.143Z","etag":null,"topics":["c","php","php5","php5-extension","php7","php7-extension","phpextension"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yulonghu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-03-19T06:47:44.000Z","updated_at":"2024-03-16T11:17:20.000Z","dependencies_parsed_at":"2024-07-08T00:28:37.867Z","dependency_job_id":null,"html_url":"https://github.com/yulonghu/sg","commit_stats":null,"previous_names":["yulonghu/sg"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yulonghu%2Fsg","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yulonghu%2Fsg/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yulonghu%2Fsg/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yulonghu%2Fsg/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yulonghu","download_url":"https://codeload.github.com/yulonghu/sg/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248103915,"owners_count":21048244,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c","php","php5","php5-extension","php7","php7-extension","phpextension"],"created_at":"2024-09-24T19:58:30.632Z","updated_at":"2025-04-09T19:51:01.388Z","avatar_url":"https://github.com/yulonghu.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SG - PHP Syntax Sugar Extension\n\n[![Build Status](https://travis-ci.org/yulonghu/sg.svg?branch=master)](https://travis-ci.org/yulonghu/sg)\n\n[中文文档](https://github.com/yulonghu/sg/blob/master/README_ZH.md) | English Document\n\n### Table of Contents\n\n* [Introduction](#introduction)\n* [Features](#features)\n* [Install](#install)\n    * [Supported Version](#supported-version)\n    * [DownLoad](#download)\n    * [Compile SG in Linux](#compile-sg-in-linux)\n        * [Add the follow information to your php.ini](#add-the-follow-information-to-your-phpini)\n* [Help Manual](#help-manual)\n    * [API](#api)\n        * [global $variable](#global-variable)\n        * [Static Methods](#static-methods)\n    * [Inis(php.ini)](#inisphpini)\n    * [Hash Map](#hash-map)\n    * [Example](#example)\n        * [global $variable](#global-variable-1)\n            * [sg.global_level = 1](#sgglobal_level--1)\n            * [sg.global_level = 0](#sgglobal_level--0)\n            * [sg.func_name](#sgfunc_name)\n        * [Static Methods](#static-methods-1)\n            * [sg::get/set/has/del()](#sggetsethasdel)\n            * [array sg::all(void)](#array-sgallvoid)\n            * [sg.func_name](#sgfunc_name-1)\n* [License](#license)\n\n### Introduction\n\n[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.\n\nThese superglobal variables managed by SG are: $_SERVER, $_GET, $_POST, $_FILES, $_COOKIE, $_SESSION, $_REQUEST, $_ENV.\n\nThe version v3.0.1 support read raw data from the request body. In the case of POST requests.\n\nVery important point: it is very simple.\n\n### Features\n- Simple, Fast, Lightweight\n- Access PHP superglobals zero-copy, Synchronously update PHP Superglobals variables\n- Support custom call function name, Default call [PHP trim](http://php.net/manual/en/function.trim.php)\n- Solve the problem of undefined series when using PHP Superglobals variables (Undefined variable, Undefined offset)\n- Use static function method, Replace the PHP array dimension with a decimal point\n- Use global statement, Replace the PHP array dimension with a underline\n- Support for global $variable the option configuration, Default level one lookup\n- Support for read raw data from the POST requests\n\n## Install\n### Supported Version\n- PHP 5.4 +\n- PHP 7.0 +\n\n### DownLoad\n```\ngit clone https://github.com/yulonghu/sg.git\n```\n\n### Compile SG in Linux\n```\n$ /path/to/php/bin/phpize\n$ ./configure --with-php-config=/path/to/php/bin/php-config\n$ make \u0026\u0026 make install\n```\n\n#### Add the follow information to your php.ini\n```ini\nextension=sg.so\n\n[sg]\nsg.enable = On\n```\n\nRestart the php-fpm.\n\n## Help Manual\n\n### API\n\n#### global $variable\n\n```php\nglobal $g_key, $p_key, $c_key, $s_key, $f_key, $n_key, $e_key, $r_key\n```\n\n#### Static Methods\n```php\narray sg::all(void)\nmixed sg::get(string $key [, mixed $default_value = null])\nbool sg::set(string $key, mixed $value)\nbool sg::has(string $key)\nbool sg::del(string $key [, mixed $... ])\nmixed sg::getRaw([mixed $default_value = null [, int $maxlen]])\nmixed sg::getCache(string $key [, mixed $default_value = null])\n```\n\n### Inis(php.ini)\n| Options | Permission | Type | Default | Desc |\n|---|---|---|---|---|\n|sg.enable|PHP_INI_SYSTEM|bool|0| 0 Trun-Off 1 Turn-On|\n|sg.global_level|PHP_INI_SYSTEM|bool|1| 1 Limit Level 0 Unlimited Level|\n|sg.func_name|PHP_INI_ALL|char|trim| [PHP trim](http://php.net/manual/en/function.trim.php), Support for custom function|\n\n### Hash Map\n- When management PHP sessions (MapKey = n), First call the function session_start()\n\n|PHP Predefined Superglobals|SG Key|global statement| Method Example|\n| ------ | ------ | ------ | ------ |\n|$GLOBALS|-|-|sg::all()|\n|$_SERVER|s|global $s|sg::get/set/has/del('s')|\n|$_GET|g|global $g|sg::get/set/has/del('g')|\n|$_POST|p|global $p|sg::get/set/has/del('p')|\n|$_FILES|f|global $f|sg::get/set/has/del('f')|\n|$_COOKIE|c|global $c|sg::get/set/has/del('c')|\n|$_SESSION|n|global $n|sg::get/set/has/del('n')|\n|$_REQUEST|r|global $r|sg::get/set/has/del('r')|\n|$_ENV|e|global $e|sg::get/set/has/del('e')|\n\n### Example\n\n#### global $variable\n##### sg.global_level = 1\n\n```php\n\u003c?php\n\n$_GET['key'] = 'GET_test_key';\n\nfunction testGlobal()\n{\n    global $g_key;\n\n    var_dump($g_key);\n\n    $g_key = 'NEW_GET_test_key';\n}\n\ntestGlobal();\n\nvar_dump(sg::get('g.key'));\nvar_dump($GLOBALS['g_key']);\nvar_dump($g_key);\nvar_dump($_GET['key']);\n```\n\nThe above example will output:\n\n```txt\nstring(12) \"GET_test_key\"\nstring(16) \"NEW_GET_test_key\"\nstring(16) \"NEW_GET_test_key\"\nstring(16) \"NEW_GET_test_key\"\nstring(16) \"NEW_GET_test_key\"\n```\n\n##### sg.global_level = 0\n\n```php\n\u003c?php\n\n$_GET['key']['key1']['key2'] = 'GET_test_key';\n\nfunction testGlobal()\n{\n    global $g_key_key1_key2;\n}\n\ntestGlobal();\n\nvar_dump(sg::get('g.key.key1.key2'));\nvar_dump($GLOBALS['g_key_key1_key2']);\nvar_dump($g_key_key1_key2);\nvar_dump($_GET['key']['key1']['key2']);\n```\n\nThe above example will output:\n\n```txt\nstring(12) \"GET_test_key\"\nstring(12) \"GET_test_key\"\nstring(12) \"GET_test_key\"\nstring(12) \"GET_test_key\"\n```\n##### sg.func_name\n\n```php\n\u003c?php\n\nini_set('sg.func_name', 'decryptTest');\n\n$_POST['key'] = 'IEEgQmFuYW5hIA==';\n\nfunction decryptTest($data)\n{\n    return trim(base64_decode($data));\n}\n\nglobal $p_key;\n\nvar_dump($p_key);\n```\n\nThe above example will output:\n\n```txt\nstring(8) \"A Banana\"\n```\n\n#### Static Methods\n##### sg::get/set/has/del()\n\n```php\n\u003c?php\n\n$key = 'test';\n$val = 'A Banana';\n\necho \"------------------start\\n\";\nvar_dump(sg::get($key));\nvar_dump(sg::get($key, 'def'));\nvar_dump(sg::has($key));\n\necho \"------------------set\\n\";\nvar_dump(sg::set($key, $val));\n\necho \"------------------get\\n\";\nvar_dump(sg::get($key));\nvar_dump(sg::get($key, 'def'));\nvar_dump(sg::has($key));\n\necho \"------------------del\\n\";\nvar_dump(sg::del($key));\n\necho \"------------------get\\n\";\nvar_dump(sg::get($key));\nvar_dump(sg::has($key));\n```\n\nThe above example will output:\n\n```txt\n------------------start\nNULL\nstring(3) \"def\"\nbool(false)\n------------------set\nbool(true)\n------------------get\nstring(8) \"A banana\"\nstring(8) \"A banana\"\nbool(true)\n------------------del\nbool(true)\n------------------get\nNULL\nbool(false)\n```\n\n##### array sg::all(void)\nSame as [$GLOBALS](https://www.php.net/manual/zh/reserved.variables.globals.php)\n\n##### sg.func_name\n```php\n\u003c?php\n\nini_set('sg.func_name', 'decryptTest');\n\nfunction decryptTest($data)\n{\n    return trim(base64_decode($data));\n}\n\nfunction encryptTest($data) \n{\n    return base64_encode(trim($data));\n}\n\nsg::set('user', encryptTest(' A Banana '));\nvar_dump(sg::get('user'));\n```\n\nThe above example will output:\n\n```txt\nstring(8) \"A Banana\"\n```\n\n## License\nSG is open source software under the [PHP License v3.01](http://www.php.net/license/3_01.txt)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyulonghu%2Fsg","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyulonghu%2Fsg","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyulonghu%2Fsg/lists"}