您好,欢迎访问一九零五行业门户网

PHP扩展开发教程(总结)

这篇文章主要介绍了php扩展开发教程的相关资料,需要的朋友可以参考下
php是一种解释型的语言,对于用户而言,我们精心的控制内存意味着easier prototyping和更少的崩溃!当我们深入到内核之后,所有的安全防线都已经被越过,最终还是要依赖于真正有责任心的软件工程师来保证系统的稳定运行。
1、线程安全宏定义
在tsrm/tsrm.h文件中有如下定义
#define tsrmls_fetch()       void ***tsrm_ls = (void ***) ts_resource_ex(0, null)
#define tsrmls_fetch_from_ctx(ctx) void ***tsrm_ls = (void ***) ctx
#define tsrmls_set_ctx(ctx)   ctx = (void ***) tsrm_ls
#define tsrmg(id, type, element)   (((type) (*((void ***) tsrm_ls))[tsrm_unshuffle_rsrc_id(id)])->element)
#define tsrmls_d   void ***tsrm_ls
#define tsrmls_dc  , tsrmls_d
#define tsrmls_c   tsrm_ls
#define tsrmls_cc  , tsrmls_c
在ext/xsl/php_xsl.h有这么一段话
/* in every utility function you add that needs to use variables.                                                                   
   in php_xsl_globals, call tsrm_fetch(); after declaring other.
   variables used by that function, or better yet, pass in tsrmls_cc
   after the last function argument and declare your utility function
   with tsrmls_dc after the last declared argument.  always refer to
   the globals in your function as xsl_g(variable).  you are.
   encouraged to rename these macros something shorter, see
   examples in any other php module directory.
*/
1.在方法定义时加上tsrmls_d(如果方法没有参数用这个)或者tsrmls_dc(有1个以上的参数)
2.在方法调用时用tsrmls_c(如果方法没有参数用这个)或者tsrmls_cc(有1个以上的参数)
应该可以这样理解
第一个后缀字母d表示定义,即d=define,第一个后缀字母c表示调用,即c=call,而第二个后缀字母c是不是表示逗号呢? c=comma (逗号)
tsrmls_d就是定义了,所以是  void ***tsrm_ls
tsrmls_dc是带逗号的定义,所以是 , void ***tsrm_ls
tsrmls_c是调用,即tsrm_ls
tsrmls_cc是调用并带逗号,即 ,tsrm_ls
所以一个是形参、一个是实参
可以这样使用
int php_myext_action(int action_id, char *message tsrmls_dc);
php_myext_action(42, the meaning of life tsrmls_cc);
一般推荐使用tsrm_ls指针定义的方式来保证线程安全
tsrmls_fetch调用需要一定的处理时间。这在单次迭代中并不明显,但是随着你的线程数增多,随着你调用tsrmls_fetch()的点的增多,你的扩展就会显现出这个瓶颈。因此,请谨慎的使用它。 注意:为了和c++编译器兼容,请确保将tsrmls_fetch()和所有变量定义放在给定块作用域的顶部(任何其他语句之前)。因为tsrmls_fetch()宏自身有多种不同的解析方式,因此最好将它作为变量定义的最后一行
2、php的生命周期
php的最多的两种运行模式是web模式、cli模式,无论哪种模式,php工作原理都是一样的,作为一种sapi运行。
1、当我们在终端敲入php这个命令的时候,它使用的是cli。
它就像一个web服务器一样来支持php完成这个请求,请求完成后再重新把控制权交给终端。
2、当使用apache作为宿主时,当一个请求到来时,php会来支持完成这个请求
php_minit_function  初始化module时运行
php_mshutdown_function  当module被卸载时运行
php_rinit_function  当一个request请求初始化时运行
php_rshutdown_function  当一个request请求结束时运行
php_minfo_function  这个是设置phpinfo中这个模块的信息
php_ginit_function  初始化全局变量时
php_gshutdown_function  释放全局变量时
比如php_ginit_function
php_ginit_function(test){ /** 初始化全局变量 */}//对应的c代码void zm_globals_ctor_test (zend_test_globals *test_globals tsrmls_dc){ /** 初始化全局变量 */}//在线程退出时,需要将之前自己申请的资源释放时,可以使用 php_gshutdown_function来注册析构函数。php_gshutdown_function(test){ /** 清除全局变量 */}//对应的c代码void zm_globals_dtor_test (zend_test_globals *test_globals tsrmls_dc){ /** 清除全局变量 */}
这里有一段代码,可以测试一下
int minit_time;php_minit_function(test){ minit_time = time(null); return success;}php_mshutdown_function(test){ file *fp=fopen(mshutdown.txt,a+); fprintf(fp,%ld\n,time(null)); fclose(fp); return success;}int rinit_time;php_rinit_function(test){ rinit_time = time(null); return success;}php_rshutdown_function(test){ file *fp=fopen(rshutdown.txt,a+); fprintf(fp,%ld\n,time(null)); fclose(fp); return success;}php_minfo_function(test){ php_info_print_table_start(); php_info_print_table_header(, module info, enabled); php_info_print_table_end(); /* remove comments if you have entries in php.ini display_ini_entries(); */}php_function(test){ php_printf(%d,time_of_minit); php_printf(%d,time_of_rinit); return;}
3、段错误调试
其它类似信息

推荐信息