145 lines
4.8 KiB
Markdown
145 lines
4.8 KiB
Markdown
---
|
|
title: PHP 扩展开发(一)-骨架
|
|
---
|
|
|
|
> 学习了这么久的 php,还一直停留在 CURD 也太捞了,来接触一下扩展开发
|
|
> 官方的文档:[http://php.net/manual/zh/internals2.php](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](http://php.net/manual/zh/internals2.buildsys.skeleton.php)
|
|
|
|
首先我们要利用 php 给我们提供的 ext_skel 脚本工具生成我们扩展的骨架,这个文件一般在 php 的源码的 ext 目录下面
|
|
|
|
这里我弄一个扩展名字为**study**的扩展 --extname 这个参数为扩展名字,还有其他的参数可以在上面的链接文档中看到
|
|
|
|
```shell
|
|
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 权限
|
|
|
|
```shell
|
|
huanl@huanl-CN15S:/www/server/php/72/src/ext$ sudo chmod -R 777 study/
|
|
```
|
|
|
|
### 编译安装
|
|
|
|
#### 目录结构
|
|
|
|
之后我们进入这个扩展目录,有这些文件,官方的文档:[http://php.net/manual/zh/internals2.structure.files.php](http://php.net/manual/zh/internals2.structure.files.php)
|
|
|
|
```shell
|
|
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](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
|
|
|
|
```shell
|
|
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 文件在哪里
|
|
|
|
```shell
|
|
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`
|
|
|
|
```shell
|
|
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.
|
|
```
|