Skip to content

Commit

Permalink
chapter 12 complete
Browse files Browse the repository at this point in the history
chapter 12 complete
  • Loading branch information
zxcvdavid committed Aug 3, 2012
1 parent 731a213 commit e8c209f
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 3 deletions.
2 changes: 1 addition & 1 deletion 12.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ FG() | 文件全局变量。大多数文件I/O或相关的全局变量的数据
## links
* [目录](<preface.md>)
* 12.3 [常量](<12.3.md>)
* 12.5 [PHP语言中的超级变量](<12.5.md>)
* 12.5 [PHP语言中的超级全局变量](<12.5.md>)
## LastModified
* $Id$
88 changes: 88 additions & 0 deletions 12.5.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# 12.5 PHP语言中的超级全局变量(Superglobals)

在PHP中有一种“特殊”的全局变量,通常我们把它们称作超级全局变量,常见的比如`$_GET``$_POST``$_FILE`等等。

他们会在编译之前就声明,所以在普通的脚本中,可能无法定义其它的超级全局变量。在扩展中,最好的使用超级全局变量的是session扩展,它使用`$_SESSION`
`session_start()``session_write_close()`之间存储信息。那么是怎样定义`$_SESSION`这个超级全局变量的呢?我们来看下session扩展的`MINIT`函数实现:

````c
PHP_MINIT_FUNCTION(session) {
zend_register_auto_global("_SESSION",
sizeof("_SESSION") - 1,
NULL TSRMLS_CC);
return SUCCESS;
}
````
注意这里的第二个参数,`sizeof("_SESSION") - 1`,一定要排除标识字符串结束的\0符。
我们一起来看下`zend_register_auto_global()`这个函数在ZE2中的原型:
````c
int zend_register_auto_global(char *name, uint name_len,
zend_auto_global_callback auto_global_callback TSRMLS_DC)
````

在ZE1中,是没有`auto_global_callback`这个参数的。为了和PHP4兼容,我们仍需要像下面这样声明一个超级全局变量:

````c
PHP_MINIT_FUNCTION(sample4) {
zend_register_auto_global("_SAMPLE4", sizeof("_SAMPLE4") - 1
#ifdef ZEND_ENGINE_2
, NULL
#endif
TSRMLS_CC);
return SUCCESS;
}
````
###全局变量的回调
在ZE2中,`zend_register_auto_global()`函数的`auto_global_callback`参数接受一个自定义函数。在实践中,这样的做法可以用来避免复杂的初始化,我们来
看下面这一段代码:
````c
zend_bool php_sample4_autoglobal_callback(char *name, uint name_len TSRMLS_DC)
{
zval *sample4_val;
int i;
MAKE_STD_ZVAL(sample4_val);
array_init(sample4_val);
for(i = 0; i < 10000; i++) {
add_next_index_long(sample4_val, i);
}
ZEND_SET_SYMBOL(&EG(symbol_table), "_SAMPLE4", sample4_val);
return 0;
}
PHP_MINIT_FUNCTION(sample4) {
zend_register_auto_global("_SAMPLE4", sizeof("_SAMPLE4") - 1
#ifdef ZEND_ENGINE_2
, php_sample4_autoglobal_callback
#endif
TSRMLS_CC);
return SUCCESS;
}
````

不幸的是,这样的设计打破了PHP4和ZE1的规则,它们不支持这样的回调。所以,为了兼容它们,我们要在每个脚本开始的时候去调用我们的回调函数(RINIT):

````c
PHP_RINIT_FUNCTION(sample4) {
#ifndef ZEND_ENGINE_2
php_sample4_autoglobal_callback("_SAMPLE4",
sizeof("_SAMPLE4") - 1,
TSRMLS_CC);
#endif
return SUCCESS;
}
````
## links
* [目录](<preface.md>)
* 12.4 [PHP扩展中的全局变量](<12.4.md>)
* 12.6 [小结](<12.6.md>)
## LastModified
* $Id$
11 changes: 11 additions & 0 deletions 12.6.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# 12.6 小结

通过本章的课程,我们深入了解了PHP的生命周期,常量、全局变量和超级全局变量的定义和使用。在下一章中,你会学会如何声明和使用的php.ini值。

## links
* [目录](<preface.md>)
* 12.5 [PHP语言中的超级全局变量](<12.5.md>)
* 13.md [设置INI](<13.md>)

## LastModified
* $Id$
2 changes: 1 addition & 1 deletion 12.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* [2. MINFO与phpinfo](<12.2.md>)
* [3. 常量](<12.3.md>)
* [4. PHP扩展中的全局变量](<12.4.md>)
* [5. PHP语言中的超级变量](<12.5.md>)
* [5. PHP语言中的超级全局变量](<12.5.md>)
* [6. 小结](<12.6.md>)


Expand Down
2 changes: 1 addition & 1 deletion preface.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
* 2. [MINFO与phpinfo](12.2.md)
* 3. [常量](12.3.md)
* 4. [PHP扩展中的全局变量](12.4.md)
* 5. [PHP语言中的超级变量](12.5.md)
* 5. [PHP语言中的超级全局变量](12.5.md)
* 6. [小结](12.6.md)
* 13. INI Settings
* 1. Declaring and Accessing INI Settings
Expand Down

0 comments on commit e8c209f

Please sign in to comment.