简称: SG, 不区分大小写, 全称 Super Globals
简短的命名方式讨人喜欢, 包括写入、读取、删除、检查四个功能项, 全局变量生命周期直到PHP脚本执行结束时(OR 收到退出指令时)才释放。
SG类随着框架启动默认注册三个子元素(GPC):
使用SG类读写全局变量会影响PHP全局变量的原始值。
这里提供了两种方式操作全局变量:
在开启命名空间情况下(asf.use_namespace=1)类名为 Asf\Sg
<?php final class Asf_Sg { public static $inputs = array('get' => array(), 'post' => array(), 'cookie' => array()) public static boolean has(string $name) public static mixed get(string $name [, mixed $default_value = null]) public static boolean set(string $name, mixed $value) public static boolean del(string $name) }
Check for global variables exists, Support for decimal point lookup
Get a global variable value, Can be used anywhere. Support for decimal point lookup
Returns a 'null' default value if the '$name' is not found
If '$name' is found and is a string, the function php_trim is called automatically
set a global variable value
delete a global variable
<?php use Asf\Sg; $name = 'test'; $str = 'A apple'; Sg::$inputs['what'] = $str; var_dump(Sg::$inputs['what']); // string(7) "A apple" var_dump(Sg::get($name)); // NULL var_dump(Sg::set($name, 'abc123')); // bool(true) var_dump(Sg::has($name)); // bool(true) var_dump(Sg::get($name)); // string(6) "abc123" var_dump(Sg::del($name)); // bool(true) var_dump(Sg::get($name)); // NULL var_dump(Sg::has($name)); // bool(false) Sg::$inputs['post']['list']['product'] = 'asf'; var_dump(Sg::get('post.list.product')); // string(3) "asf" var_dump(Sg::has('post.list.product')); // bool(true) var_dump(Sg::get('post.list.cat', 'dvalue')); // string(6) "dvalue" Sg::set($name, ' trim_test '); var_dump(Sg::get($name)); // string(9) "trim_test" unset($name); sg::set('cat.0', 100); // Sg::$inputs['cat'][1] = 200; sg::set('cat.1', 200); // Sg::$inputs['cat'][0] = 100; var_dump(sg::get('cat.0')); // int(100) var_dump(sg::get('cat.1')); // int(200)
<?php use Asf\Sg; /* Recommend Short name */ sg::get('g'); sg::get('p'); sg::get('c'); /* Not Recommend */ sg::$inputs['get']; sg::$inputs['post']; sg::$inputs['cookie']; sg::get('get'); sg::get('post'); sg::get('cookie'); /* Get sg::inputs['get']['user'] OR $_GET['user'] */ sg::get('g.user'); /* Get sg::inputs['post']['user'] OR $_POST['user'] */ sg::get('p.uesr'); /* Get sg::inputs['cookie']['user'] OR $_COOKIE['user'] */ sg::get('c.user');
<?php use Asf\Sg; $cookie = new Asf_Http_Cookie(['path' => '/', 'domain' => 'box3.cn', 'expire' => 3600, 'secure' => 1, 'httponly' => 1, 'prefix' => 'Asf_']); Sg::set('cookie', $cookie); Sg::get('cookie');