我尝试过的:
//...
zend_long dest;
if (UNEXPECTED(!zend_parse_arg_long(arg, &dest, NULL, 0, 0))) {
zend_verify_arg_error(E_RECOVERABLE_ERROR, zf, arg_num, "be of the type integer", "", zend_zval_type_name(arg), "", arg);
}
zval_ptr_dtor(arg);
ZVAL_LONG(arg, dest);
//...
问题是,如果 arg
是带有格式错误的数字的文字字符串,例如 "10x"
,引擎会发出通知:
Notice: A non well formed numeric value encountered in...
我真正想要的是能够像下面的 PHP 用户态代码一样转换 arg
:
(int) "10x" // evaluates to 10, no NOTICE
I'm still crawling through the zend API so any help on how to find a good (updated) PHP internals reference or general advice is welcome.
最佳答案
您可以使用 zval_get_long
函数执行整数转换而不修改原始值:
zend_long lval = zval_get_long(zv);
如果你想改变现有 zval 的类型,你可以使用 convert_to_long
函数:
convert_to_long(zv);
// Z_TYPE_P(zv) == IS_LONG now
如果 zv
是引用,convert_to_long
将在转换前解包引用(因此 zv
将不再是引用)。更有可能的是你想取消引用它(所以引用仍然存在,但 zv
指向它的内部值):
ZVAL_DEREF(zv);
convert_to_long(zv);
请注意,在 PHP 7 中,在使用 convert_to_long
之前无需执行分离。
关于c - 如何使用 zend 引擎 API 对 (int) 之类的值进行类型转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27979077/