Home>Article>Backend Development> First experience using php8’s extended arginfo generation tool

First experience using php8’s extended arginfo generation tool

藏色散人
藏色散人 forward
2020-09-16 17:03:16 3428browse

php8 provides a very convenient tool for generating extension function or class parameter information.

You only need to maintain a copy ofxyz.stub.php, and you can use tools to generatexyz_arginfo.h.

There is no doubt that this method lowers the threshold for the development and extension ofphperand makes it easier to maintain.

Get started experience:

Generate extended skeleton.

cd ext php ext_skel.php --ext test

Add a function and changetest.stub.php.


       

Regeneratetest_arginfo.h.

php ../../build/gen_stub.php test.stub.php

Related commits can be clicked here (https://github.com/php/php-src/compare/master...nikic:php-stubs )

Write a simple extension example to implement theallandanyfunctions in python through PHP extension.

Preparation.
  • Download the latest source code of php
  • Already installed php
Generate the extended skeleton.
cd ext php ext_skel.php --ext python
Write the function prototype and editpython.stub.php.

       
Generatepython_arginfo.hbased onpython.stub.php.
php ../../build/gen_stub.php python.stub.php
To implement function logic, editpython.c.
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); }
Write unit tests, edit002.phptand003.phpt, create new004.phptand005.phpt.
--TEST-- Check all function true case --SKIPIF--  --FILE--  --EXPECT-- bool(true) bool(true) bool(true)
--TEST-- Check all function false case --SKIPIF--  --FILE--  --EXPECT-- bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false)
--TEST-- Check any function true case --SKIPIF-- 03337722efea1711f56b8de85a57c3f3 --FILE-- 9aca998af19012e49a0511600d6999f0 --EXPECT-- bool(true) bool(true) bool(true) bool(true)
--TEST-- Check all function false case --SKIPIF-- 03337722efea1711f56b8de85a57c3f3 --FILE-- adcfae2c6b929629deb6b746091dd12c --EXPECT-- bool(false) bool(false) bool(false) bool(false) bool(false)
Compile, test and install
./configure && make make test sudo make install
Add to php.ini
php -i | grep ini # 定位你的php.ini文件

Join

extension=python.so

Check if successful

php -m | grep python
Actual test
php -r "var_dump(all([]));“ php -r "var_dump(any([]));"

PHP8 has added a lot of useful macros and features.

The above is the detailed content of First experience using php8’s extended arginfo generation tool. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete