What is the method to create extension in php7
PHP扩展是编译库,它允许在您的PHP代码中使用特定的功能。今天我们就来给大家介绍一下php创建一个扩展的方式,有需要的小伙伴可以参考参考。
将实现如下功能:
<?phpecho say(); ?>
输出内容:
$ php ./test.php $ hello word
在扩展中实现一个say方法,调用say方法后,输出 hello word。
第一步:生成代码
PHP为我们提供了生成基本代码的工具 ext_skel。这个工具在PHP源代码的./ext目录下。
$ cd php_src/ext/ $ ./ext_skel --extname=say
extname参数的值就是扩展名称。执行ext_skel命令后,这样在当前目录下会生成一个与扩展名一样的目录。
第二步,修改config.m4配置文件
config.m4的作用就是配合phpize工具生成configure文件。configure文件是用于环境检测的。检测扩展编译运行所需的环境是否满足。现在我们开始修改config.m4文件。
$ cd ./say $ vim ./config.m4
打开,config.m4文件后,你会发现这样一段文字。
dnl If your extension references something external, use with: dnl PHP_ARG_WITH(say, for say support, dnl Make sure that the comment is aligned: dnl [ --with-say Include say support]) dnl Otherwise use enable: dnl PHP_ARG_ENABLE(say, whether to enable say support, dnl Make sure that the comment is aligned: dnl [ --enable-say Enable say support])
其中,dnl 是注释符号。上面的代码说,如果你所编写的扩展如果依赖其它的扩展或者lib库,需要去掉PHP_ARG_WITH相关代码的注释。否则,去掉 PHP_ARG_ENABLE 相关代码段的注释。我们编写的扩展不需要依赖其他的扩展和lib库。因此,我们去掉PHP_ARG_ENABLE前面的注释。去掉注释后的代码如下:
dnl If your extension references something external, use with: dnl PHP_ARG_WITH(say, for say support, dnl Make sure that the comment is aligned: dnl [ --with-say Include say support]) dnl Otherwise use enable: PHP_ARG_ENABLE(say, whether to enable say support, Make sure that the comment is aligned: [ --enable-say Enable say support])
第三步,代码实现
修改say.c文件。实现say方法。
找到PHP_FUNCTION(confirm_say_compiled),在其上面增加如下代码:
PHP_FUNCTION(confirm_say_compiled) { zend_string *strg; strg = strpprintf(0, "hello word"); RETURN_STR(strg); }
找到 PHP_FE(confirm_say_compiled, 在上面增加如下代码:
PHP_FE(say, NULL)
修改后的代码如下:
const zend_function_entry say_functions[] = { PHP_FE(say, NULL) /* For testing, remove later. */ PHP_FE(confirm_say_compiled, NULL) /* For testing, remove later. */ PHP_FE_END /* Must be the last line in say_functions[] */ }; /* }}} */
第四步,编译安装
编译扩展的步骤如下:
$ phpize $ ./configure $ make && make install
修改php.ini文件,增加如下代码:
[say] extension = say.so
然后执行,php -m 命令。在输出的内容中,你会看到say字样。
第五步,调用测试
自己写一个脚本,调用say方法。看输出的内容是否符合预期。
推荐学习:php视频教程
The above is the detailed content of What is the method to create extension in php7. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to install the mongo extension in php7.0: 1. Create the mongodb user group and user; 2. Download the mongodb source code package and place the source code package in the "/usr/local/src/" directory; 3. Enter "src/" directory; 4. Unzip the source code package; 5. Create the mongodb file directory; 6. Copy the files to the "mongodb/" directory; 7. Create the mongodb configuration file and modify the configuration.

In php5, we can use the fsockopen() function to detect the TCP port. This function can be used to open a network connection and perform some network communication. But in php7, the fsockopen() function may encounter some problems, such as being unable to open the port, unable to connect to the server, etc. In order to solve this problem, we can use the socket_create() function and socket_connect() function to detect the TCP port.

To resolve the plugin not showing installed issue in PHP 7.0: Check the plugin configuration and enable the plugin. Restart PHP to apply configuration changes. Check the plugin file permissions to make sure they are correct. Install missing dependencies to ensure the plugin functions properly. If all other steps fail, rebuild PHP. Other possible causes include incompatible plugin versions, loading the wrong version, or PHP configuration issues.

How to install and deploy php7.0: 1. Go to the PHP official website to download the installation version corresponding to the local system; 2. Extract the downloaded zip file to the specified directory; 3. Open the command line window and go to the "E:\php7" directory Just run the "php -v" command.

Common solutions for PHP server environments include ensuring that the correct PHP version is installed and that relevant files have been copied to the module directory. Disable SELinux temporarily or permanently. Check and configure PHP.ini to ensure that necessary extensions have been added and set up correctly. Start or restart the PHP-FPM service. Check the DNS settings for resolution issues.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Causes and solutions for errors when using PECL to install extensions in Docker environment When using Docker environment, we often encounter some headaches...

Compared with PHP7, PHP8 has some advantages and improvements in terms of performance, new features and syntax improvements, type system, error handling and extensions. However, choosing which version to use depends on your specific needs and project circumstances. Detailed introduction: 1. Performance improvement, PHP8 introduces the Just-in-Time (JIT) compiler, which can improve the execution speed of the code; 2. New features and syntax improvements, PHP8 supports the declaration of named parameters and optional parameters, making functions Calling is more flexible; anonymous classes, type declarations of properties, etc. are introduced.
