search
HomeBackend DevelopmentPHP8First experience using php8's extended arginfo generation tool

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

You only need to maintain a copy of xyz.stub.php, and you can use tools to generate xyz_arginfo.h.

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

Get started experience:

Generate extended skeleton.

cd ext
php ext_skel.php --ext test

Add a function and change test.stub.php.

<?php

/** @generate-function-entries */

function test1(): void {}

function test2(string $str = ""): string {}

function test3(int $integer = 123): int {}

Regenerate test_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 the all and any functions 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 edit python.stub.php.
<?php

/** @generate-function-entries */

function all(array $arr): bool {}

function any(array $arr): bool {}
Generate python_arginfo.h based on python.stub.php.
php ../../build/gen_stub.php python.stub.php
To implement function logic, edit python.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, edit 002.phpt and 003.phpt, create new 004.phpt and 005.phpt.
--TEST--
Check all function true case
--SKIPIF--
<?php
if (!extension_loaded(&#39;python&#39;)) {
    echo &#39;skip&#39;;
}
?>
--FILE--
<?php
var_dump(all([]));
var_dump(all([1]));
var_dump(all([-1, 1, &#39;1&#39;]));
?>
--EXPECT--
bool(true)
bool(true)
bool(true)
--TEST--
Check all function false case
--SKIPIF--
<?php
if (!extension_loaded(&#39;python&#39;)) {
    echo &#39;skip&#39;;
}
?>
--FILE--
<?php
var_dump(all([&#39;0&#39;]));
var_dump(all([0]));
var_dump(all([&#39;&#39;]));
var_dump(all([false]));
var_dump(all([1, -1, 100, false]));
var_dump(all([0, -1, 100, 1]));
var_dump(all([&#39;1&#39;, -1, &#39;&#39;, 100, 1]));
?>
--EXPECT--
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
--TEST--
Check any function true case
--SKIPIF--
<?php if (!extension_loaded(&#39;python&#39;)) {
    echo &#39;skip&#39;;
}
?>
--FILE--
<?php var_dump(any([&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;]));
var_dump(any([[&#39;a&#39;, &#39;b&#39;, &#39;&#39;, &#39;d&#39;]]));
var_dump(any([[&#39;&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;]]));
var_dump(any([[&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;&#39;]]));
?>
--EXPECT--
bool(true)
bool(true)
bool(true)
bool(true)
--TEST--
Check all function false case
--SKIPIF--
<?php if (!extension_loaded(&#39;python&#39;)) {
    echo &#39;skip&#39;;
}
?>
--FILE--
<?php var_dump(any([&#39;0&#39;]));
var_dump(any([0]));
var_dump(any([&#39;&#39;]));
var_dump(any([false]));
var_dump(any([0, &#39;0&#39;, &#39;&#39;, false]));
?>
--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. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor