PHP7扩展开发之hello word
本文是以PHP7作为基础,讲解如何从零开始创建一个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方法。看输出的内容是否符合预期。
完整代码下载
完整的扩展源代码下载。
原文链接: PHP7扩展开发之hello word,转载请注明来源!

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

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

Laravel simplifies HTTP verb handling in incoming requests, streamlining diverse operation management within your applications. The method() and isMethod() methods efficiently identify and validate request types. This feature is crucial for building

The Storage::download method of the Laravel framework provides a concise API for safely handling file downloads while managing abstractions of file storage. Here is an example of using Storage::download() in the example controller:
