search
HomeBackend DevelopmentPHP TutorialQuickly develop a PHP extension graphic tutorial_PHP tutorial
Quickly develop a PHP extension graphic tutorial_PHP tutorialJul 21, 2016 pm 03:48 PM
phpfunctionGraphics and textdevelopfastExpandTutorialofneed

需求:比如开发一个叫做 heiyeluren 的扩展,扩展里就一个函数 heiyeluren_test(),输入一个字符串,函数返回:Your input string: xxxxx。
要求:了解C/C++编程,熟悉PHP编程
环境:下载一份php对应版本的源码,我这里是 php-5.2.6,先正常安装php,假设我们的php安装在 /usr/local/php 目录,源码在 /root/soft/php/php-5.2.6/,现在开始!
步骤一:生成扩展框架


cd /root/soft/php/php-5.2.6/ext
./ext_skel --extname=heiyeluren
cd /root/soft/php/php-5.2.6/ext/heiyeluren
vi config.m4
打开文件后去掉 dnl ,获得下面的信息:
PHP_ARG_ENABLE(heiyeluren, whether to enable heiyeluren support,
[  --enable-heiyeluren           Enable heiyeluren support])

保存退出.
(图01)
Quickly develop a PHP extension graphic tutorial_PHP tutorial

 

 

第二步:编写代码

vi php_heiyeluren.h
找到:PHP_FUNCTION(confirm_heiyeluren_compiled); ,新增一行:
PHP_FUNCTION(heiyeluren_test);
保存退出。
(图02)
Quickly develop a PHP extension graphic tutorial_PHP tutorial


vi heiyeluren.c
数组里增加我们的函数,找到 zend_function_entry heiyeluren_functions[],增加:
PHP_FE(heiyeluren, NULL)
(图03)

Quickly develop a PHP extension graphic tutorial_PHP tutorial

 

再到 heiyeluren.c 文件最后面增加如下代码:
PHP_FUNCTION(heiyeluren_test)
{
    char *arg = NULL;
    int arg_len, len;
    char *strg;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
        return;
    }

    len = spprintf(&strg, 0, "Your input string: %s\n", arg);
    RETURN_STRINGL(strg, len, 0);
}
保存退出。
(图04)

Quickly develop a PHP extension graphic tutorial_PHP tutorial

 

 

 

第三步:编译安装

cd /root/soft/php/php-5.2.6/ext/heiyeluren
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make test
make install

现在看看是不是有个 /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/heiyeluren.so
编辑php.ini,把扩展加入进去:
vi /usr/local/php/lib/php.ini
在[PHP]模块下增加:
extension = heiyeluren.so
保存退出。
(图05)

Quickly develop a PHP extension graphic tutorial_PHP tutorial

 

注意:如果你不存在扩展文件目录,或者安装报错,那么可以自行建立这个目录,然后把扩展拷贝到目录下,然后记得把 php.ini 文件中的 extension_dir 修改为该目录:
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"
(图06)

Quickly develop a PHP extension graphic tutorial_PHP tutorial

 

 

第四步:检查安装结果
现在看看模块加载了没有:
/usr/local/php/bin/php -m,应该会打印出:
[PHP Modules]
...
heiyeluren
...
[Zend Modules]


然后重启apache,输出 phpinfo() ,应该能够看到:
heiyeluren
heiyeluren support enabled

(图07)

Quickly develop a PHP extension graphic tutorial_PHP tutorial

 

看看函数是否存在并且调用,在web目录下建立:heiyeluren.php
echo "

";<br>print_r(get_loaded_extensions());<br>print_r(get_extension_funcs('heiyeluren'));<br>echo heiyeluren_test('My first php extension');<br>echo "
";
?>

访问apache,应该能够看到:
Array
(
    ...
    [33] => heiyeluren
)
Array
(
    [0] => confirm_heiyeluren_compiled
    [1] => heiyeluren_test
)
Your input string: heiyeluren
(图08)

Quickly develop a PHP extension graphic tutorial_PHP tutorial


Extension production successful!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/319691.htmlTechArticleRequirements: For example, develop an extension called heiyeluren. There is a function heiyeluren_test() in the extension, input a string, The function returns: Your input string: xxxxx. Requirements: Understand...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
理解SpringBoot和SpringMVC之间的差异及比较理解SpringBoot和SpringMVC之间的差异及比较Dec 29, 2023 am 09:20 AM

对比SpringBoot与SpringMVC,了解它们的差异随着Java开发的不断发展,Spring框架已经成为了许多开发人员和企业的首选。在Spring的生态系统中,SpringBoot和SpringMVC是两个非常重要的组件。虽然它们都是基于Spring框架的,但在功能和使用方式上却有一些区别。本文将重点对比一下SpringBoot与Sprin

WordPress 网站搭建指南:快速搭建个人网站WordPress 网站搭建指南:快速搭建个人网站Mar 04, 2024 pm 04:39 PM

WordPress网站搭建指南:快速搭建个人网站随着数字化时代的到来,拥有一个个人网站已经成为了一种时尚和必要。而WordPress作为最受欢迎的网站搭建工具,让搭建个人网站变得更加容易和便捷。本文将为大家提供一个快速搭建个人网站的指南,包含具体的代码示例,希望可以帮助到想要拥有自己网站的朋友们。第一步:购买域名和主机在开始搭建个人网站之前,首先要购买自己

Vue3中的生命周期函数:快速掌握Vue3的生命周期Vue3中的生命周期函数:快速掌握Vue3的生命周期Jun 18, 2023 am 08:20 AM

Vue3是目前前端界最热门的框架之一,而Vue3的生命周期函数是Vue3中非常重要的一部分。Vue3的生命周期函数可以让我们实现在特定的时机触发特定的事件,增强了组件的高度可控性。本文将从Vue3的生命周期函数的基本概念、各个生命周期函数的作用和使用方法以及实现案例等方面进行详细探究和讲解,帮助读者快速掌握Vue3的生命周期函数。一、Vue3的生命周期函数的

win10电脑怎么快速切屏win10电脑怎么快速切屏Jul 10, 2023 am 08:21 AM

电脑怎么切屏?在使用电脑的时候,有的朋友会使用两个甚至三个显示屏,但是在使用的时候,就会遇到需要切换屏幕的问题,那么电脑怎么切屏呢?一些朋友不知道电脑快速切屏方法,所以本期将教大家win10电脑怎么快速切屏。win10电脑怎么快速切屏?具体的方法如下:1、外接显示屏以后,同时按下【Fn】+【F4】或者【win】+【P】即可选择外接显示器。2、第二种方法是,在桌面空白处鼠标右键,然后选择【屏幕分辨率】。3、然后在【多显示器】中,就能够切换屏幕了。以上就是小编带来的win10电脑怎么快速切屏的全部内

前端开发中sessionStorage的优点及应用案例分析前端开发中sessionStorage的优点及应用案例分析Jan 11, 2024 pm 02:51 PM

sessionStorage在前端开发中的优势与应用案例分析随着Web应用的发展,前端开发的需求也越来越多样化。前端开发人员需要使用各种工具和技术来提高用户体验,其中,sessionStorage是一个非常有用的工具。本文将介绍sessionStorage在前端开发中的优势,以及几个具体的应用案例。sessionStorage是HTML5提供的一种本地存储方

分享PyCharm中快速注释代码的技巧,提高工作效率分享PyCharm中快速注释代码的技巧,提高工作效率Jan 04, 2024 pm 12:02 PM

效率提升!PyCharm中快速注释代码的方法分享在日常的软件开发工作中,我们经常需要注释掉一部分代码进行调试或者调整。如果手动逐行添加注释,这无疑会增加我们的工作量和耗费时间。而PyCharm作为一款强大的Python集成开发环境,提供了快速注释代码的功能,大大提升了我们的开发效率。本文将分享一些在PyCharm中快速注释代码的方法,并提供具体的代码示例。单

win7如何快速截屏win7如何快速截屏Jun 29, 2023 am 11:19 AM

win7如何快速截屏?win7系统之中有着很多便捷操作功能,可以为各位提供非常多样化的便捷服务。很多win7系统的用户在使用电脑的过程中,想要通过win7系统之中的快捷键进行截屏,但是却不清楚具体的快捷键是哪些,因此无法正常使用,那么,这些快捷截屏键究竟是哪些呢?下面小编就为各位带来win7快速截屏键介绍。win7快速截屏键介绍1、按Prtsc键截图这样获取的是整个电脑屏幕的内容,按Prtsc键后,可以直接打开画图工具,接粘贴使用。也可以粘贴在QQ聊天框或者Word文档中,之后再选择保存即可。2

VUE3初学者必备的快速开发入门指南VUE3初学者必备的快速开发入门指南Jun 15, 2023 pm 04:38 PM

VUE3初学者必备的快速开发入门指南Vue是一款流行的JavaScript框架,它的易用性、高度定制性和快速开发模式使得它在前端开发中广受欢迎。而最新的Vue3则推出了更多强大的特性,包括性能优化、TypeScript支持、CompositionAPI以及更好的自定义渲染器等等。本篇文章将为Vue3初学者提供一份快速开发入门指南,帮助你快速上手Vue3开发

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
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment