7

PHP扩展开发——准备

 2 years ago
source link: https://anhoder.github.io/post/2es0g0FAq/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

PHP扩展开发——准备

发布于 2021-08-01 | 分类于 PHP扩展 PHP | 2分钟 | 319字数

在开发扩展之前,需要做一些准备工作:脚手架使用、环境配置。

生成基本骨架

首先,我们需要使用官方提供的ext_skel脚本来生成扩展的大致骨架和基础代码。

./ext_skel.php --ext ext_test # ext_test为扩展名称
复制代码

这个脚本在php源码中可以找到,具体路径为php-src/ext/ext_skel

php 7.3之前是用shell写的脚本,之后是用php写的。

执行完上述命令后,就会生成扩展骨架:

ext_test/
├── config.m4       # unix编译配置
├── config.w32      # windows编译配置
├── ext_test.c      # 主要c代码
├── php_ext_test.h  # 主要头文件
└── tests           # 测试
    ├── 001.phpt
    ├── 002.phpt
    └── 003.phpt
复制代码

这里我是用JetBrains家的CLion作为开发环境,具体配置步骤为:

1. 创建CMakeLists.txt文件

点击Create CMakeLists.txt会生成如下的文件内容:

cmake_minimum_required(VERSION 3.20)
project(ext_test C)

set(CMAKE_C_STANDARD 11)

include_directories(.)

add_executable(ext_test
        ext_test.c
        php_ext_test.h)
复制代码

2. 加载PHP头文件

此时打开C文件ext_test.c会有一些报红,我们需要在CMakeLists.txt中增加如下代码,将PHP的头文件也加载进来即可:

include_directories(/usr/local/include/php)
include_directories(/usr/local/include/php/main)
include_directories(/usr/local/include/php/Zend)
复制代码

准备工作完成了,后面就会开始实现扩展的一些具体功能。

Related Issues not found

Please contact @anhoder to initialize the comment


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK