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
php8怎么加mysql扩展php8怎么加mysql扩展Oct 07, 2023 pm 03:31 PM

php8加mysql扩展的步骤是:1、安装MySQL客户端库;2、安装PHP 8的开发工具;3、下载MySQL扩展源代码;4、编译和安装MySQL扩展;5、启用MySQL扩展;6、重启Web服务器即可。

php5和php8有什么区别php5和php8有什么区别Sep 25, 2023 pm 01:34 PM

php5和php8的区别在性能、语言结构、类型系统、错误处理、异步编程、标准库函数和安全性等方面。详细介绍:1、性能提升,PHP8相对于PHP5来说在性能方面有了巨大的提升,PHP8引入了JIT编译器,可以对一些高频执行的代码进行编译和优化,从而提高运行速度;2、语言结构改进,PHP8引入了一些新的语言结构和功能,PHP8支持命名参数,允许开发者通过参数名而不是参数顺序等等。

图文详解apache2.4+php8.0的安装配置方法图文详解apache2.4+php8.0的安装配置方法Dec 06, 2022 pm 04:53 PM

本文给大家介绍如何安装apache2.4,以及如何配置php8.0,文中附有图文详细步骤,下面就带大家一起看看怎么安装配置apache2.4+php8.0吧~

php8怎么连接数据库php8怎么连接数据库Nov 16, 2023 pm 02:41 PM

PHP8可以使用mysqli和PDO来连接数据库。详细介绍:1、使用mysqli连接数据库,通过传入数据库服务器名称、用户名、密码和数据库名称来进行连接。然后,使用`connect_error`属性来检查连接是否成功,如果连接失败,则输出错误信息。最后,通过调用`close()`方法关闭连接;2、使用PDO连接数据库,通过传入数据库服务器名称、密码和数据库名称来进行连接等等。

php8数据类型怎么转换php8数据类型怎么转换Nov 16, 2023 pm 02:51 PM

php8数据类型的方法有字符串转换为整数、整数转换为字符串、字符串转换为浮点数、浮点数转换为字符串、数组转换为字符串、字符串转换为数组、布尔值转换为整数、整数转换为布尔值和变量类型判断和转换。详细介绍:1、字符串转换为整数包括intval()函数和(int)强制类型转换;2、整数转换为字符串包括strval()函数和(string)强制类型转换;3、字符串转换为浮点数等等。

一文深入了解 PHP 8 中的 JIT一文深入了解 PHP 8 中的 JITApr 25, 2022 pm 08:46 PM

本篇文章带大家了解一下PHP 8 中的 JIT,并聊聊JIT 是怎么参与解释流程的,希望对大家有所帮助!

php8到底有哪些性能提升php8到底有哪些性能提升Dec 21, 2023 pm 02:44 PM

php8提高的性能包括:1、JIT编译器的引入;2、函数调用的优化;3、垃圾回收机制的改进;4、类型系统的改进;5、新的语言特性;6、优化字符串处理;7、改进数组处理;8、引入新的内存管理机制;9、优化代码生成。详细介绍:1、JIT编译器的引入,PHP8引入了JIT编译器,这是一种动态编译技术,能够将PHP代码转换为机器码,以便更高效地执行;2、函数调用的优化等等。

php8有什么特性php8有什么特性Dec 21, 2023 pm 02:54 PM

php8的特性:1、JIT编译器;2、命名参数;3、联合类型;4、注解;5、构造函数属性提升;6、match表达式;7、nullsafe运算符;8、改进类型系统;9、错误处理改进;10、一致性改进。详细介绍:1、JIT编译器,PHP8引入了即时编译技术,将PHP代码编译成本机机器码,从而提高程序的执行速度;2、命名参数,PHP8支持命名参数,允许在函数调用时使用参数名称等等。

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.