首页
导航
博客
电子书
算法
众创
代码
随贴
关于我们
您好,欢迎来到码863代码分享网! 请
[登录]
/
[注册]
搜 索
标题:
*
140
字
TAG标签:
(用空格隔开)
30
字
恢复历史版本:
请选择分类
html
python
javascript
php
sql
c
c++
c#
java
plain
所有人可见
仅自己可见
编辑器:UEditor
编辑器:TinyMCE
编辑器:Editor.md
HTML转MD
HTML转MD2
<p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);">之前发过几篇文章都是关于一些运维方向的。想必比较枯燥!那么今天正好有时间聊点儿关于开发的有趣事情。</p><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);">说起PHP这门语言,做web开发的朋友应该不会太陌生,经常会被调侃为“世界上最好的语言”。至于为啥说PHP是世界上最好的语言,在这里我就不唠叨了。因为也不是咱们今天的主题。大家有兴趣可以自己研究研究。</p><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);">在工作中我会经常使用一些计算机语言来开发一些软件,刚才说的PHP就在其中。标题中的C语言也是其中一种语言。(说起C语言大家都不陌生)</p><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);">那么有时候一当一种语言满足不了我的需求的时候就可能会考虑与其他的语言协作。那么今天我就说一下如何使用C语言开发一个PHP扩展。</p><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);">首先我先抛出几个问题:</p><ol class="list-paddingleft-2" style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;padding:0px 0px 0px 1.625em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);"><li><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;">学了C语言到底能有啥用<br style="border:0px solid rgb(229,231,235);" /></p></li><li><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;">为啥要用C语言给php写扩展</p></li><li><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;">如何用C语言给php写扩展(重点)<br style="border:0px solid rgb(229,231,235);" /></p></li></ol><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);">一、学了C语言有啥用?</p><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);">这个问题在我上学的时候我就问过自己,尴尬的是直到毕业后工作了才搞明白一丢丢。后来在我做学院外聘讲师的时候也有学生问过我,直到现在也都有人会起问我。</p><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);">那么今天我在这里简单说一下有啥用:</p><ol class="list-paddingleft-2" style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;padding:0px 0px 0px 1.625em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);"><li><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;">首先我们要明白C语言为啥被称为高级语言中的低级语言。在这里我就用大白话和大家说:用它写一个程序比汇编语言效率高,比高级语言(java、php等)效率低。<br style="border:0px solid rgb(229,231,235);" /></p></li><li><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;">学习了c语言可以更好的对计算机内存进行管理写出高质量的程序,也可以更好的对计算机底层、原理进行一些深刻的认识。这样对后期的开发工作很有帮助。</p></li><li><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;">学习c语言是一个打基础的过程,基础打好了后期编写程序的过程中也会如鱼得水。</p></li></ol><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);">二、为啥要用C语言给php写扩展?</p><ol class="list-paddingleft-2" style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;padding:0px 0px 0px 1.625em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);"><li><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;">用原生php编写的程序性能达到了一定的瓶颈。<br style="border:0px solid rgb(229,231,235);" /></p></li><li><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;">用于替换php中反复用到的一些公共的用php写的函数,如:接口数据输出、数据的接收方式等。</p></li><li><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;">不同系统中的数据的共享,编写消息队列中间件、数据库连接池等。</p></li><li><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;">其他还有很多,可以在实际工作中体会。</p></li></ol><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);">三、如何用c语言给php写扩展(重点)</p><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);">好了说了半天了,那我们就开始用c语言编写一个php扩展吧。</p><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);">首先我们需要具备的知识:C语言基础语法、php基础语法、Linux基础、php的模块编译安装</p><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);">有了以上的基础,我们就可以开始编写扩展了。我把内容分为五个步骤:</p><ol class="list-paddingleft-2" style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;padding:0px 0px 0px 1.625em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);"><li><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;">下载php源码、生成php扩展框架<br style="border:0px solid rgb(229,231,235);" /></p></li><li><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;">php扩展框架讲解</p></li><li><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;">编写几个简单的php扩展函数</p></li><li><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;">编译安装</p></li><li><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;">测试运行</p></li></ol><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);">一、mac下打开终端</p><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);"> 下载:wget <a href="https://maimai.cn/n/online/link?target=https%3A%2F%2Fwww.php.net%2Fdistributions%2Fphp-7.1.33.tar.gz" style="border:0px solid rgb(229,231,235);">https://www.php.net/distributions/php-7.1.33.tar.gz</a><br style="border:0px solid rgb(229,231,235);" /></p><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);"> 解压:tar -zxvf <a href="https://maimai.cn/n/online/link?target=https%3A%2F%2Fwww.php.net%2Fdistributions%2Fphp-7.1.33.tar.gz" style="border:0px solid rgb(229,231,235);">php-7.1.33.tar.gz</a></p><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);"> 进入扩展目录:cd php-7.1.33-src/ext/</p><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);"> 执行:./ext_skel 会有一些说明,如图一。我们看到有一个选项:--extname=module选项,解释说明了module为扩展名称,现在我们执行 ./ext_skel --extname=test_add。这里的test_ddd就是我们的扩展名字</p><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);text-align:center;"><img alt="1697178177650895.png" src="/ueditor/php/upload/image/20231013/1697178177650895.png" style="border:0px solid rgb(229,231,235);vertical-align:middle;margin-top:2em;margin-bottom:2em;" title="1697178177650895.png" border="0" vspace="0" /></p><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);">二、扩展框架讲解</p><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);"> 执行完./ext_skel --extname=test_add后,在我门的目录下生成了一个test_add的文件夹,这个文件夹中的各种文件就是我们php的扩展框架。我们可以用vscode打开他,看看目录结构。</p><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);text-align:center;"><img alt="1697178443772560.png" src="/ueditor/php/upload/image/20231013/1697178443772560.png" style="border:0px solid rgb(229,231,235);vertical-align:middle;margin-top:2em;margin-bottom:2em;" title="1697178443772560.png" border="0" vspace="0" /></p><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);">这里我们看到有几个文件还有一个文件夹,这里我着重介绍几个文件,首先是config.m4,这个文件是用来生成configure脚本、Makefile等文件。还有是test_add.c这个就是我们扩展的主文件,在里面可以写一些我们的业务代码。<span style="border:0px solid rgb(229,231,235);font-weight:600;">php_test_add.h</span>这个一看就是头文件,里面主要定义了一些宏,方便我们扩展的开发。</p><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);">好了,既然<span style="border:0px solid rgb(229,231,235);font-weight:600;">test_add.c是扩展的主文件</span>,那么我们打开他看一下,在这里面我们可以看到头部引入了一些.h的头文件。其中有:<span style="border:0px solid rgb(229,231,235);font-weight:600;">php.h、php_ini.h</span>等、还有刚才提到的<span style="border:0px solid rgb(229,231,235);font-weight:600;">php_test_add.h</span>。</p><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);">往下看有几个宏,主要的几个是:<span style="border:0px solid rgb(229,231,235);font-weight:600;">PHP_FUNCTION、PHP_MINIT_FUNCTION、PHP_MSHUTDOWN_FUNCTION、PHP_RINIT_FUNCTION、PHP_RSHUTDOWN_FUNCTION、PHP_MINFO_FUNCTION</span>。</p><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);">另外还有一个test_add_functions。</p><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);">三、编写几个简单的php扩展函数</p><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);">今天我们要用的主要有一个宏PHP_FUNCTION,还有test_add_functions。其他的宏后面有机会在和大家聊。因为我们今天的主要目的是写几个扩展函数,让跑起来。</p><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);">1)PHP_FUNCTION宏:</p><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);">我们可以把我们要定义的函数名字作为参数然后写入这个宏中,如:</p><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);text-align:center;"><img alt="1697178541317742.png" src="/ueditor/php/upload/image/20231013/1697178541317742.png" style="border:0px solid rgb(229,231,235);vertical-align:middle;margin-top:2em;margin-bottom:2em;" title="1697178541317742.png" border="0" vspace="0" /></p><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);">在上图中我把之前默认的函数名字改为了test_add_1后又新增了一个函数test_add_2。然后我们会在PHP_FUNCTION(test_add_2)中把这个函数完善一下(这个函数的主要功能是让两个数相加然后把结果返回给用户,就这么简单)</p><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);">1-1)首先我们定义两个长整型:long a, b;</p><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);">1-2)然后通过:zend_parse_parameters函数接收他们</p><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);">1-3)定义一个long c,然后让a, b相加赋值给c</p><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);">1-4)输出c<br style="border:0px solid rgb(229,231,235);" /></p><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);text-align:center;"><img alt="1697178640714062.png" src="/ueditor/php/upload/image/20231013/1697178640714062.png" style="border:0px solid rgb(229,231,235);vertical-align:middle;margin-top:2em;margin-bottom:2em;" title="1697178640714062.png" border="0" vspace="0" /></p><p><span style="color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';background-color:rgb(255,255,255);">2)test_add_functions:(php8.1搜索test1,在.stub文件中加函数)</span></p><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);">我们在它的上面可以看到一句话:Every user visible function must have an entry in test_add_functions[]. 也就是说我们在上面创建了函数还不能让客户端PHP使用必须在PHP_FE这个宏里指定了才可以</p><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);text-align:center;"><img alt="1697179124111200.jpeg" src="/ueditor/php/upload/image/20231013/1697179124111200.jpeg" style="border:0px solid rgb(229,231,235);vertical-align:middle;margin-top:2em;margin-bottom:2em;" title="1697179124111200.jpeg" border="0" vspace="0" /></p><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);">以上就是我们定义两个函数的名字:test_add_1、test_add_2</p><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);">四、编译安装</p><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);">我们开始要编译了,在编译之前我们需要生成一下Makefile等文件,下来执行:phpize,这时候我们的扩展目录下会多出好多文件,其中就有configure,好的我们执行./configure,最后我们再执行:make && make install。看到下图说明我们就编译安装成功了。</p><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);text-align:center;"><img src="/ueditor/php/upload/image/20231013/1697179271445470.png" alt="1697179271445470.png" /></p><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);">最后我们再php.ini中配置我们的扩展</p><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);">最后一行添加:extension = test_add.so</p><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);">重启Apache<br style="border:0px solid rgb(229,231,235);" /></p><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);">五、测试运行</p><ol class="list-paddingleft-2" style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;padding:0px 0px 0px 1.625em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);"><li><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;">我们执行php -m会看到刚才我们编写的扩展,下图:</p><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;text-align:center;"><img src="/ueditor/php/upload/image/20231013/1697179378786832.png" alt="1697179378786832.png" /></p></li><li><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;">我们在终端输入 php -r "test_add_2(1,2);"<br style="border:0px solid rgb(229,231,235);" /></p><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;text-align:center;"><img src="/ueditor/php/upload/image/20231013/1697179681960377.png" alt="1697179681960377.png" /><br style="border:0px solid rgb(229,231,235);" /></p></li></ol><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);"><br style="border:0px solid rgb(229,231,235);" /></p><p style="border:0px solid rgb(229,231,235);margin-top:1.25em;margin-bottom:1.25em;color:rgb(55,65,81);font-family:'ui-sans-serif', 'system-ui', '-apple-system', BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';white-space:normal;background-color:rgb(255,255,255);">好了,这里就结束了,后期有时间会和大家分享其他宏、变量、数据类型的定义等。</p><p><br /></p>
CopyRight 2002~2023 精通2100网 联系邮箱:qqtxt@163.com
版权所有:精通2100网
湘ICP备2023018646号-1
MYSQl共执行 4 个查询,用时 0.0019969940185547 秒,PHP脚本用时 0.004912 秒,占用内存 0.714 MB,Gzip 已启用