爱编码的Farmer

  • 主页
  • 编程开发
  • 运营维护
  • 随心笔记
  • 留言页面
  • 打赏
爱编码的Farmer
我是Farmer,我为自己代言
  1. 首页
  2. 编程开发
  3. 教程笔记
  4. 正文

PHP扩展开发(一)---骨架

2018年9月21日 4657点热度 3人点赞 0条评论

学习了这么久的php,还一直停留在CURD也太捞了,来接触一下扩展开发
官方的文档:http://php.net/manual/zh/internals2.php 可以mark一下

环境

  • php7.2
  • ubuntu18.04
  • gcc 7.3.0
  • make 4.1

开始

ext_skel

http://php.net/manual/zh/internals2.buildsys.skeleton.php

首先我们要利用php给我们提供的ext_skel脚本工具生成我们扩展的骨架,这个文件一般在 php的源码的ext目录下面

这里我弄一个扩展名字为study的扩展 --extname这个参数为扩展名字,还有其他的参数可以在上面的链接文档中看到

huanl@huanl-CN15S:/www/server/php/72/src/ext$ sudo ./ext_skel --extname=study
Creating directory study
Creating basic files: config.m4 config.w32 .gitignore study.c php_study.h CREDITS EXPERIMENTAL tests/001.phpt study.php [done].

To use your new extension, you will have to execute the following steps:

1.  $ cd ..
2.  $ vi ext/study/config.m4
3.  $ ./buildconf
4.  $ ./configure --[with|enable]-study
5.  $ make
6.  $ ./sapi/cli/php -f ext/study/study.php
7.  $ vi ext/study/study.c
8.  $ make

Repeat steps 3-6 until you are satisfied with ext/study/config.m4 and
step 6 confirms that your module is compiled into PHP. Then, start writing
code and repeat the last two steps as often as necessary.

这里创建完成之后,就多了一个study的目录,我们可以进入这个扩展目录,进行一些操作,这里的话,因为我的用户没有权限,所以我直接的给了777权限

huanl@huanl-CN15S:/www/server/php/72/src/ext$ sudo chmod -R 777 study/

编译安装

目录结构

之后我们进入这个扩展目录,有这些文件,官方的文档:http://php.net/manual/zh/internals2.structure.files.php

huanl@huanl-CN15S:/www/server/php/72/src/ext$ cd study/
huanl@huanl-CN15S:/www/server/php/72/src/ext/study$ ls
config.m4   CREDITS       php_study.h  study.php
config.w32  EXPERIMENTAL  study.c      tests

等下我们需要修改config.m4文件,这相当于是一个编译配置的文档,是Unix下的,还有一个config.w32看名字就知道是Windows下的

study.c和php_study.h这是依照我们的扩展名称来帮我们生成的两个源文件,包括了一些宏的定义和函数声明等等

study.php可以用php cli来测试我们的扩展是否安装成功

config.m4

http://php.net/manual/zh/internals2.buildsys.configunix.php

要进行一下修改,是动态编译成so库还是静态编译进php里面,这里我们扩展开发自然是动态编译成so库,不然还得重新编译php

去掉前面的dnl,例如下面这个

dnl If your extension references something external, use with:
# 编译成so库
PHP_ARG_WITH(study, for study support,
Make sure that the comment is aligned:
[  --with-study             Include study support])

dnl Otherwise use enable:
# 静态编译
dnl PHP_ARG_ENABLE(study, whether to enable study support,
dnl Make sure that the comment is aligned:
dnl [  --enable-study           Enable study support])

编译

我们需要用phpize生成编译的配置的文件configure,然后make,make install就完成了,make install 的时候注意用sudo

huanl@huanl-CN15S:/www/server/php/72/src/ext/study$ phpize
Configuring for:
PHP Api Version:         20170718
Zend Module Api No:      20170718
Zend Extension Api No:   320170718

huanl@huanl-CN15S:/www/server/php/72/src/ext/study$ ./configure --with-php-config=/www/server/php/72/bin/php-config
# 这里注意配置php的配置文件

huanl@huanl-CN15S:/www/server/php/72/src/ext/study$ make

huanl@huanl-CN15S:/www/server/php/72/src/ext/study$ sudo make install
Installing shared extensions:     /www/server/php/72/lib/php/extensions/no-debug-non-zts-20170718/

配置

完成之后我们要在php.ini文件中加载扩展,不然我们运行php study.php的时候不会成功

这样可以看ini文件在哪里

huanl@huanl-CN15S:/www/server/php/72/src/ext/study$ php --ini
Configuration File (php.ini) Path: /www/server/php/72/etc
Loaded Configuration File:         /www/server/php/72/etc/php.ini
Scan for additional .ini files in: (none)
Additional .ini files parsed:      (none)

增加一行

extension=study.so

完成

这时候我们执行目录下的那个study.php

huanl@huanl-CN15S:/www/server/php/72/src/ext/study$ php study.php 
Functions available in the test extension:
confirm_study_compiled

Congratulations! You have successfully modified ext/study/config.m4. Module study is now compiled into PHP.
标签: 暂无
最后更新:2018年9月21日

Farmer

不敢说什么

点赞
< 上一篇
下一篇 >

文章评论

取消回复

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据。

COPYRIGHT © 2021 icodef.com. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

湘ICP备19008073号