forked from walu/phpbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chapter 12 complete
- Loading branch information
Showing
5 changed files
with
102 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters