c - 如何使用 zend 引擎 API 对 (int) 之类的值进行类型转换?

标签 c php-internals

我尝试过的:

//...
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/

相关文章:

c - MacOS 从 Mavericks Lion 兼容的可执行文件编译

c - 使用字符串表和打印部分名称

PHP:为什么零长度字符串上的数组语法将字符串转换为数组?

string - 为什么 PHP 不对字符串使用内部智能字符串?

php - 为什么 PHP 的 hash_equals() 函数中的参数顺序很重要?

没有 strlen() 的 PHP 字符串长度

c++ - Metro 的东西在什么桌面上运行?

c - 如何从结构中打印函数指针的值

c - 如何分割和保存

php - 为什么不支持将 PHP 函数导入当前命名空间