Home Backend Development PHP Tutorial php扩展如何嵌入到php框架中

php扩展如何嵌入到php框架中

Jun 23, 2016 pm 02:29 PM

编写一个简单的扩展示例

使用php内置工具生成自定义扩展的框架。该工具在ext文件夹下面

./ext_skel --extname=wully

 

Ext_skel的主要参数(这里面只列举了最常用的两个)

  --extname=module   module is the name of your extension【必须有的】

生成模块的名称,会自动在ext内建立extname文件夹,最常用的

  --proto=file       file contains prototypes of functions to create【可选】

函数原型定义文件

 

官方说明地址:

http://www.php.net/manual/en/internals2.buildsys.skeleton.php

 

修正ext/wully/config.m4

 

修正后,运行:

./buildconf --force

./configure --enable-wully

Make & make install 及可以安装成功

可以通过运行 ext/wully/wully.php来校验是否安装成功

 

扩展的内部实现


依旧以生成的wully模块为例,这个模块的提供的外部函数在

const zend_function_entry wully_functions[]这里面陈列(wully.c),我们可以看到示例中定义的函数为

PHP_FE(confirm_wully_compiled,  NULL)

这个模块的外部函数和模块在php请求各个阶段的调用逻辑是通过

wully_module_entry(zend_module_entry)结构传给php框架的

 

php扩展函数的实现逻辑

每一个扩展函数都是通过PHP_FUNCTION来实现的,例如我们的示例,通过PHP_FUNCTION(confirm_wully_compiled) 来实现的

 

内部如何获取php函数的参数

生成的源码函数为:

zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len)

这个是一个参数,且参数类型为字符串的情况

 

调用的原型为:

zend_parse_parameters(int num_args TSRMLS_DC, char *type_spec, …);

有两个参数我们要特别注意下:

第一个参数为该函数获取的参数个数

第三个参数为php函数传过来的参数类型,下表是一个对应关系图

后面的参数则是参数的值,及参数的大小,第二个参数对应于线程安全,这里面先不关心

 

参数类型对应表

类型指定符

对应的C类型

描述

l

long

符号整数

d

double

浮点数

s

char *, int

二进制字符串,长度

b

zend_bool

逻辑型(1或0)

r

zval *

资源(文件指针,数据库连接等)

a

zval *

联合数组

o

zval *

任何类型的对象

O

zval *

指定类型的对象。需要提供目标对象的类类型

z

zval *

无任何操作的zval

 

 

再看一个三个参数,一个为字符串,两个为整数

zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sll", &arg, &arg_len,&n,&m)

 

 PHP_FUNCTION展开模式:

#define PHP_FUNCTION        ZEND_FUNCTION

#define ZEND_FUNCTION(name)        ZEND_NAMED_FUNCTION(ZEND_FN(name))

#define ZEND_FN(name) zif_##name

#define ZEND_NAMED_FUNCTION(name)        void name(INTERNAL_FUNCTION_PARAMETERS)

#define INTERNAL_FUNCTION_PARAMETERS int ht, zval *return_value, zval **return_value_ptr, zval *this_ptr, int return_value_used TSRMLS_DC

 

提供外部函数列表的展开模式,即PHP_FE

#define PHP_FE          ZEND_FE

#define ZEND_FE(name, arg_info)        ZEND_FENTRY(name, ZEND_FN(name), arg_info, 0)

#define ZEND_FENTRY(zend_name, name, arg_info, flags)        { #zend_name, name, arg_info, (zend_uint) (sizeof(arg_info)/sizeof(struct _zend_arg_info)-1), flags },

 

 

按照php处理请求的各个阶段我们分为 mint rint   script rshutdown mshutdown,我们自定义的外部扩展对请求的各个阶段也可以进行相应的干预。


各个阶段的干预接口为:


MINIT阶段:模块初始化阶段

对应PHP_MINIT(wully)的实现

在sapi的start阶段,调用zend_startup_module

ZEND_API int zend_startup_module(zend_module_entry *module)执行两个动作注册模块,启动模块

注册模块

zend_register_internal_module(module TSRMLS_CC)

把module放入 module_registry 中,其中 module_registry 为一个全局的hash tab

 

启动模块

zend_startup_module_ex(module TSRMLS_CC) == SUCCESS

检测依赖是否就绪,然后运行该module的 PHP_MINIT函数

 

MINIT宏展开模式:

#define PHP_MINIT                ZEND_MODULE_STARTUP_N

#define ZEND_MINIT                        ZEND_MODULE_STARTUP_N

#define ZEND_MODULE_STARTUP_N(module)       zm_startup_##module

 

RINIT阶段:请求初始化

 

对应PHP_RINIT(wully)的实现

int php_request_startup(TSRMLS_D)

void zend_activate_modules(TSRMLS_D)

int module_registry_request_startup(zend_module_entry *module TSRMLS_DC)

module->request_startup_func(module->type, module->module_number TSRMLS_CC)

 

RINIT宏展开模式

#define PHP_RINIT  ZEND_MODULE_ACTIVATE_N

#define ZEND_RINIT  ZEND_MODULE_ACTIVATE_N

#define ZEND_MODULE_ACTIVATE_N(module)  zm_activate_##module

 

script阶段

我们的PHP_FUNCTION执行在script阶段,他们作为php语言编译和解释的一部分,纳入到opcode的执行中

 

PHP_RSHUTDOWN阶段:请求关闭

对应PHP_RSHUTDOWN(wully))的实现

 

void php_request_shutdown(void *dummy)

void zend_deactivate_modules(TSRMLS_D)

int module_registry_cleanup(zend_module_entry *module TSRMLS_DC)

module->request_shutdown_func(module->type, module->module_number TSRMLS_CC)

 

PHP_RSHUTDOWN        宏展开模式

#define PHP_RSHUTDOWN            ZEND_MODULE_DEACTIVATE_N

#define ZEND_MODULE_DEACTIVATE_N(module)        zm_deactivate_##module

 

PHP_MSHUTDOWN阶段:模块关闭

对应PHP_MSHUTDOWN(wully))的实现

在zend_startup中注册模块销毁时的析构函数

int zend_startup(zend_utility_functions *utility_functions, char **extensions TSRMLS_DC)

zend_hash_init_ex(&module_registry, 50, NULL, ZEND_MODULE_DTOR, 1, 0);

#define ZEND_MODULE_DTOR (void (*)(void *)) module_destructor

void module_destructor(zend_module_entry *module)

module->module_shutdown_func(module->type, module->module_number TSRMLS_CC);

 

PHP_MSHUTDOWN           宏展开模式

#define PHP_MSHUTDOWN            ZEND_MODULE_SHUTDOWN_N

#define ZEND_MODULE_SHUTDOWN_N(module)  zm_shutdown_##module

 

PHP_MINFO跟前面的宏意义有所不同,主要用于在phpinfo中显示模块的信息

#define PHP_MINFO                ZEND_MODULE_INFO_N

#define ZEND_MINFO                        ZEND_MODULE_INFO_N

Statement of this Website
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

What are HTTP request methods (GET, POST, PUT, DELETE, etc.) and when should each be used? What are HTTP request methods (GET, POST, PUT, DELETE, etc.) and when should each be used? Apr 09, 2025 am 12:09 AM

HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

Explain the difference between self::, parent::, and static:: in PHP OOP. Explain the difference between self::, parent::, and static:: in PHP OOP. Apr 09, 2025 am 12:04 AM

In PHPOOP, self:: refers to the current class, parent:: refers to the parent class, static:: is used for late static binding. 1.self:: is used for static method and constant calls, but does not support late static binding. 2.parent:: is used for subclasses to call parent class methods, and private methods cannot be accessed. 3.static:: supports late static binding, suitable for inheritance and polymorphism, but may affect the readability of the code.

How does PHP handle file uploads securely? How does PHP handle file uploads securely? Apr 10, 2025 am 09:37 AM

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

How does PHP type hinting work, including scalar types, return types, union types, and nullable types? How does PHP type hinting work, including scalar types, return types, union types, and nullable types? Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles