Skip to content

Commit d4dceb1

Browse files
committed
Update 04.md
加入扩展代码
1 parent d00cde1 commit d4dceb1

File tree

1 file changed

+129
-2
lines changed

1 file changed

+129
-2
lines changed

04.md

Lines changed: 129 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,135 @@
11
# INI参数设置
22

3-
1,创建一个新的扩展
3+
## 创建一个新的扩展
4+
5+
+ 生成扩展
6+
7+
```
8+
./ext_skel --extname=inis
9+
```
10+
11+
+ config.m4
12+
13+
```
14+
PHP_ARG_ENABLE(inis, whether to enable inis support,
15+
[ --enable-inis Enable inis support])
16+
17+
if test "$PHP_INIS" != "no"; then
18+
PHP_NEW_EXTENSION(inis, inis.c, $ext_shared)
19+
fi
20+
```
21+
22+
+ php_inis.h
23+
24+
```
25+
ZEND_BEGIN_MODULE_GLOBALS(inis)
26+
char *name;
27+
long level;
28+
ZEND_END_MODULE_GLOBALS(inis)
29+
30+
#ifdef ZTS
31+
#define INIS_G(v) TSRMG(inis_globals_id, zend_inis_globals *, v)
32+
#else
33+
#define INIS_G(v) (inis_globals.v)
34+
#endif
35+
```
36+
37+
+ inis.c
38+
39+
定义全局变量
40+
41+
```
42+
ZEND_DECLARE_MODULE_GLOBALS(inis)
43+
```
44+
45+
设置INI参数
46+
47+
```
48+
PHP_INI_BEGIN()
49+
STD_PHP_INI_ENTRY("inis.name","sunzy",PHP_INI_ALL,OnUpdateString,name,zend_inis_globals,inis_globals)
50+
STD_PHP_INI_ENTRY("inis.level","1",PHP_INI_ALL,OnUpdateLong,level,zend_inis_globals,inis_globals)
51+
PHP_INI_END()
52+
```
53+
54+
初始化函数
55+
56+
```
57+
static void php_inis_init_globals(zend_inis_globals *inis_globals)
58+
{
59+
inis_globals->name="sunzy.org";
60+
inis_globals->level=10;
61+
}
62+
```
63+
64+
注册&清除INI变量
65+
66+
```
67+
PHP_MINIT_FUNCTION(inis)
68+
{
69+
REGISTER_INI_ENTRIES();
70+
return SUCCESS;
71+
}
72+
73+
PHP_MSHUTDOWN_FUNCTION(inis)
74+
{
75+
UNREGISTER_INI_ENTRIES();
76+
return SUCCESS;
77+
}
78+
```
79+
80+
自定义函数
81+
82+
```
83+
PHP_FUNCTION(inis_report)
84+
{
85+
php_printf("Name:%s\n",INIS_G(name));
86+
php_printf("Level:%d\n",INIS_G(level));
87+
}
88+
```
89+
90+
注册自定义函数
91+
92+
```
93+
const zend_function_entry inis_functions[] = {
94+
PHP_FE(inis_report,NULL)
95+
PHP_FE_END /* Must be the last line in inis_functions[] */
96+
};
97+
```
98+
99+
+ 测试
100+
101+
测试脚本
102+
103+
```
104+
$function = "inis_report";
105+
call_user_func($function);
106+
```
107+
108+
测试
109+
110+
```
111+
$ /usr/local/php5.6.9/bin/php -q inis.php
112+
Functions available in the test extension:
113+
inis_report
114+
Name:sunzy
115+
Level:1
116+
```
117+
118+
修改php.ini
119+
120+
```
121+
inis.name=test
122+
inis.level=2
123+
```
124+
125+
```
126+
$ /usr/local/php5.6.9/bin/php -q inis.php
127+
Functions available in the test extension:
128+
inis_report
129+
Name:test
130+
Level:2
131+
```
4132

5-
./ext_skel --extname=inis
6133

7134
2,宏
8135

0 commit comments

Comments
 (0)