php扩展开发(二)

本文介绍PHP扩展开发中如何使用ZEND_PARSE_PARAMETERS_START进行参数解析,包括必填与可选参数的定义方式,并通过实例展示了参数数量错误时的处理机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

php扩展开发(二)

传入参数
    zval *name = NULL   ,*height = NULL;

    //参数范围是1到2个
    ZEND_PARSE_PARAMETERS_START(1,2)
        Z_PARAM_ZVAL_DEREF(name)
        Z_PARAM_OPTIONAL
        Z_PARAM_ZVAL_DEREF(height)
    ZEND_PARSE_PARAMETERS_END_EX(zend_bailout());

这里面的参数

ZEND_PARSE_PARAMETERS_START(1, 2)

第一个参数表示必传的参数格式,第二个参数表示最多传入的参数个数。

以ZENDPARSEPARAMETERS_END();结束。

引自网址

中间是传入参数的解析。

|   Z_PARAM_OPTIONAL
a   Z_PARAM_ARRAY(dest) dest - zval*
A   Z_PARAM_ARRAY_OR_OBJECT(dest)   dest - zval*
b   Z_PARAM_BOOL(dest)  dest - zend_bool
C   Z_PARAM_CLASS(dest) dest - zend_class_entry*
d   Z_PARAM_DOUBLE(dest)    dest - double
f   Z_PARAM_FUNC(fci, fcc)  fci - zend_fcall_info, fcc - zend_fcall_info_cache
h   Z_PARAM_ARRAY_HT(dest)  dest - HashTable*
H   Z_PARAM_ARRAY_OR_OBJECT_HT(dest)    dest - HashTable*
l   Z_PARAM_LONG(dest)  dest - long
L   Z_PARAM_STRICT_LONG(dest)   dest - long
o   Z_PARAM_OBJECT(dest)    dest - zval*
O   Z_PARAM_OBJECT_OF_CLASS(dest, ce)   dest - zval*
p   Z_PARAM_PATH(dest, dest_len)    dest - char*, dest_len - int
P   Z_PARAM_PATH_STR(dest)  dest - zend_string*
r   Z_PARAM_RESOURCE(dest)  dest - zval*
s   Z_PARAM_STRING(dest, dest_len)  dest - char*, dest_len - int
S   Z_PARAM_STR(dest)   dest - zend_string*
z   Z_PARAM_ZVAL(dest)  dest - zval*
Z_PARAM_ZVAL_DEREF(dest)    dest - zval*
+   Z_PARAM_VARIADIC('+', dest, num)    dest - zval*, num int
*   Z_PARAM_VARIADIC('*', dest, num)    dest - zval*, num int

现在我们在对昨天的代码如果不加入异常处理zend_bailout(),直接执行

<?php
$config = [];
$a = new Abel_application();
$a->name=3;
$aa = $a->ourMethod1(1,2,3,4,5,6,7,8,9);
echo "\r\n";
var_dump($aa);
echo "\r\n";
//helloworld();
//var_dump($a);

运行结果

PHP Warning:  abel_application::__construct() expects at least 1 parameter, 0 given in /root/php-7.1.1/ext/abel_application/test2.php on line 3

Warning: abel_application::__construct() expects at least 1 parameter, 0 given in /root/php-7.1.1/ext/abel_application/test2.php on line 3
hello,zhang lei this is your first destrcut extension

如果加入zend_bailout() 执行代码运行结果:

PHP Warning:  abel_application::__construct() expects at least 1 parameter, 0 given in /root/php-7.1.1/ext/abel_application/test2.php on line 3

Warning: abel_application::__construct() expects at least 1 parameter, 0 given in /root/php-7.1.1/ext/abel_application/test2.php on line 3
hello,zhang lei this is your first destrcut extension

我们会发现直接释放类,结束了脚本

而PHP在遇到Fatal Error的时候,会直接zendbailout,而zendbailout会导致程序流程直接跳过上面代码段,也可以理解为直接exit了(longjmp),这就导致了userexceptionhandler没有机会发生作用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值