正文
php8提供了非常方便的扩展函数或类参数信息的生成工具。
只需要维护一份xyz.stub.php
,就可以使用工具生成 xyz_arginfo.h
。
毫无疑问,这种方式,又降低了广大 phper
开发扩展的门槛,更易维护。
上手体验
生成扩展骨架。
1 2 | cd ext php ext_skel.php --ext test |
随便添加一个函数,更改 test.stub.php
。
1 2 3 4 | <?php /** @generate-function-entries */ function test1(): void {} function test2(string $str = "" ): string {} function test3(int $integer = 123): int {} |
重新生成 test_arginfo.h
。
1 | php ../../build/gen_stub.php test.stub.php |
相关 commit 可以 点击这儿
写个简单的扩展举例,通过php扩展的方式来实现python中的all
和 any
函数。
准备工作
- 下载php最新源码
- 已经安装好php
生成扩展骨架
1 2 | cd ext php ext_skel.php --ext python |
撰写函数原型
编辑 python.stub.php
。
1 2 3 | <?php /** @generate-function-entries */ function all( array $arr ): bool {} function any( array $arr ): bool {} |
根据 python.stub.php
生成 python_arginfo.h
。
1 | php ../.. /build/gen_stub .php python.stub.php |
实现函数逻辑
编辑 python.c
。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | PHP_FUNCTION(all) { zval *input; zval *item; int result = 1, item_result = 1; HashTable *htbl; ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_ARRAY(input) ZEND_PARSE_PARAMETERS_END(); htbl = Z_ARRVAL_P(input); ZEND_HASH_FOREACH_VAL(htbl, item) { item_result = zend_is_true(item); result &= item_result; } ZEND_HASH_FOREACH_END(); RETURN_BOOL(result); } /* {{{ void any() */ PHP_FUNCTION(any) { zval *input; zval *item; int result = 0, item_result = 0; HashTable *htbl; ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_ARRAY(input) ZEND_PARSE_PARAMETERS_END(); htbl = Z_ARRVAL_P(input); ZEND_HASH_FOREACH_VAL(htbl, item) { item_result = zend_is_true(item); result |= item_result; } ZEND_HASH_FOREACH_END(); RETURN_BOOL(result); } |
编写单元测试
编辑 002.phpt
和003.phpt
, 新建 004.phpt
和005.phpt
。
1 2 3 4 5 6 7 8 9 10 | --TEST-- Check all function true case --SKIPIF-- --FILE-- --EXPECT-- bool(true) bool(true) bool(true) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | --TEST-- Check all function false case --SKIPIF-- --FILE-- --EXPECT-- bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) |
1 2 3 4 5 6 7 8 9 10 11 | --TEST-- Check any function true case --SKIPIF-- --FILE-- --EXPECT-- bool(true) bool(true) bool(true) bool(true) |
1 2 3 4 5 6 7 8 9 10 11 12 | --TEST-- Check all function false case --SKIPIF-- --FILE-- --EXPECT-- bool(false) bool(false) bool(false) bool(false) bool(false) |
编译、测试和安装
1 2 3 | . /configure && make make test sudo make install |
加入到php.ini
1 | php -i | grep ini # 定位你的php.ini文件 |
加入
1 | extension=python.so |
查看是否成功
1 | php -m | grep python |
实测
1 2 | php -r "var_dump(all([]));“ php -r "var_dump(any([]));" |
PHP8新增了蛮多有用的宏和特性。
以上就是php8扩展arginfo生成工具及工具初体验介绍的详细内容,更多关于php8扩展arginfo生成工具的资料请关注IT俱乐部其它相关文章!