File tree Expand file tree Collapse file tree 1 file changed +5
-8
lines changed Expand file tree Collapse file tree 1 file changed +5
-8
lines changed Original file line number Diff line number Diff line change @@ -11,14 +11,13 @@ function my_func(){
11
11
汇编中函数对应的是一组独立的汇编指令,然后通过call指令实现函数的调用,前面已经说过PHP编译的结果是opcode数组,与汇编指令对应,PHP用户自定义函数的实现就是将函数编译为独立的opcode数组,调用时分配独立的执行栈依次执行opcode,所以自定义函数对于zend而言并没有什么特别之处,只是将opcode进行了打包封装,实际PHP脚本中函数之外的指令整个可以认为是一个函数(或者理解为main函数更直观)。
12
12
13
13
``` php
14
- <?php
15
14
/* function main(){ */
16
15
17
16
$a = 123;
18
17
echo $a;
19
18
20
19
/* } */
21
- ?>
20
+ ```
22
21
23
22
下面具体看下PHP中函数的结构:
24
23
@@ -69,7 +68,7 @@ Zend引擎中定义了很多内部函数供用户在PHP中使用,比如:defi
69
68
70
69
#### 3.2.2.1 内部函数结构
71
70
上一节介绍` zend_function ` 为union,其中` internal_function ` 就是内部函数用到的,具体结构:
72
- ```
71
+ ``` c
73
72
// zend_complie.h
74
73
typedef struct _zend_internal_function {
75
74
/* Common elements */
@@ -127,13 +126,11 @@ PHP_MINIT_FUNCTION(xxxxxx)
127
126
```
128
127
接着编译、安装扩展,测试:
129
128
``` php
130
- <?php
131
129
qp_test();
132
- ?>
133
-
134
- 结果输出:
135
- call internal function 'qp_test'
136
130
```
131
+ 结果输出:
132
+ ` call internal function 'qp_test' `
133
+
137
134
这样一个内部函数就定义完成了。这里有一个地方需要注意的我们把这个函数注册到__ CG(function_table)__ 中去了,而不是__ EG(function_table)__ ,这是因为在` php_request_startup ` 阶段会把__ CG(function_table)__ 赋值给__ EG(function_table)__ 。
138
135
139
136
上面的过程看着比较简单,但是在实际应用中不要这样做,PHP提供给我们一套标准的定义方式,接下来看下如何在扩展中按照官方方式提供一个内部函数。
You can’t perform that action at this time.
0 commit comments