PHP扩展开发详细教程
在几种情况下,我们会需要进行PHP扩展开发:
1、PHP本身无法访问的资源或是调用,
2、通过扩展的方式可以实现处于性能和效率的考虑,
3、用C实现会更好
4、处于商业或代码保护考虑,将代码封装起来
5、为了更深入的了解PHP,从这里入门
假设有这么一个扩展,提供一个叫ccvita_string的函数。下面详细介绍怎么样制作这样一个PHP扩展:
第一步、生成代码
PHP为了扩展开发的方便,提供了一个类似代码生成器的工具ext_skel,具体可以参见说明。首先我们创建一个文件scutephp.skel,它的内容为
string scutephp_string(string str)
就是告诉ext_skel这个东西,我们要做的扩展里面有个函数叫scutephp_string。然后执行
cd usr/src/php-5.3.8/ext/<br />./ext_skel --extname=scutephp --proto=scutephp.skel<br />cd scutephp/
这时候,ccvita这个扩展的代码框架就已经出来了。
第二步、修改配置
然后修改config.m4文件将10、11、12三行最前面的dnl删除掉,就是将dnl PHP_ARG_WITH(ccvita, for ccvita support,<br />dnl Make sure that the comment is aligned:<br />dnl [ --with-scutephp Include scutephp support])
修改为
PHP_ARG_WITH(scutephp, for scutephp support,<br />Make sure that the comment is aligned:<br />[ --with-scutephp Include scutephp support])
第三步、实现功能
修改源码scutephp.c文件找到将scutephp_string这个函数修改为
PHP_FUNCTION(scutephp_string)<br />{<br /> char *str = NULL;<br /> int argc = ZEND_NUM_ARGS();<br /> int str_len;<br /> char *result;<br /><br /> if (zend_parse_parameters(argc TSRMLS_CC, "s", &str, &str_len) == FAILURE) <br /> return;<br /><br /> str_len = spprintf(&result, 0, "<a href="%.78s">Link</a>", str);<br /> RETURN_STRINGL(result, str_len, 0); <br />}
第四步、编译扩展
保存后,开始编译/usr/local/php/bin/phpize<br />./configure --with-php-config=/usr/local/php/bin/php-config<br />make
第五步、添加扩展
这时候,一切顺利的话,该扩展已经在modules/scutephp.so这个位置了。下面就是将这个扩展加入到PHP中去,让我们PHP程序可以调用到。cp modules/scutephp.so /usr/local/php/ext/<br />vim /usr/local/php/etc/php.ini<br />extension=/usr/local/php/ext/scutephp.so #在php.ini文件最后增加这一行<br />service php-fpm restart #重启PHP服务<br />cp ccvita.php /data/www/wwwroot/default/
接下来就可以访问scutephp.php这个文件,测试扩展了。

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



Is vue.js easy to learn? The answer is that it is very easy to learn and integrate with other libraries or existing projects. So, how to use vue? Today, the PHP Chinese website recommends to you the latest 5 vue.js free video tutorials in 2023 for everyone to learn.

The following is a video tutorial of the front-end UI framework - layui. There is no need to download it from third-party network disk platforms such as Xunlei and Baidu Cloud. All videos can be watched online for free. The tutorials start from the shallower to the deeper, and those with front-end basics can learn it. From installation to case explanations, they are comprehensive and detailed to help you master the layui framework faster and better!

DeepSeekAI Tool User Guide and FAQ DeepSeek is a powerful AI intelligent tool. This article will answer some common usage questions to help you get started quickly. FAQ: The difference between different access methods: There is no difference in function between web version, App version and API calls, and App is just a wrapper for web version. The local deployment uses a distillation model, which is slightly inferior to the full version of DeepSeek-R1, but the 32-bit model theoretically has 90% full version capability. What is a tavern? SillyTavern is a front-end interface that requires calling the AI model through API or Ollama. What is breaking limit

The following is a video tutorial of the PHP asynchronous communication framework Swoole. There is no need to download it from third-party platforms such as Thunder and Baidu Cloud. All are free to watch online. The tutorials are from easy to deep, and people with a basic knowledge of PHP can learn it. From installation to case explanations, they are comprehensive and detailed to help you master the Swoole framework faster and better!

This PHP Chinese website has integrated relevant video tutorials, Chinese manuals, and related selected articles for everyone, all for free! ! ! Through the videos we share, you can watch the tutorial videos for free anytime and anywhere, and there is no need to download from Thunder or Baidu Netdisk.

Many webmasters use PHPCMS for secondary development and website building. PHP Chinese website has specially launched a phpcms video tutorial. You can watch the video tutorial for free anytime and anywhere without downloading from Baidu Netdisk, which is very convenient.

DedeCMS is famous for its simplicity, practicality and open source. It is the most well-known PHP open source website management system in China and the PHP CMS system with the most users. After years of development, the current version has become the most popular one in terms of functions. , or in terms of ease of use, there has been considerable development and progress.

PHP Chinese website has compiled content related to the Empire CMS video tutorial for everyone. Contains Imperial CMS installation video tutorials, Imperial CMS development manuals, Imperial CMS template resources and some Imperial CMS technical articles.
