在第1个阶段中,解析-f参数为执行一个PHP文件,-f后面的test.php就是需要被执行的文件
这里我们将关注第3个阶段,如何执行test.php中的PHP代码。
最终是通过php_execute_script(&file_handle TSRMLS_CC)来执行PHP的脚本,这个函数定义在{PHPSRC}/main/main.c,原型为:
PHPAPI int php_execute_script(zend_file_handle *primary_file TSRMLS_DC)
file_handle的类型为zend_file_handle,这个是zend对文件句柄的一个封装,里面的内容就是和test.php相关的了。
php_execute_script最终是调用的zend_execute_scripts,这个函数定义在{PHPSRC}/Zend/zend.c,原型为:
ZEND_API int zend_execute_scripts(int type TSRMLS_DC, zval **retval, int file_count, ...)
此函数具有可变参数,可以一次执行多个PHP文件,在此函数中最核心的是调用zend_compile_file和zend_execute,
zend_compile_file是一个函数指针,其声明在{PHPSRC}/Zend/zend_compile.c:
ZEND_API zend_op_array *(*zend_compile_file)(zend_file_handle *file_handle, int type TSRMLS_DC); 在引擎初始化的时候,会将compile_file函数的地址赋值给zend_compile_file,compile_file函数定义在{PHPSRC}/Zend/zend_language_scanner.c,通过声明可以看到这个函数以zend_file_handle指针作为参数,返回一个指向zend_op_array的指针。