大网数据广告活动
PHP 语言与运行手册

C Data Handles

· 52CE 我爱测

FFI\CData objects can be used in a number of ways as a regular PHP data: C data of scalar types can be read and assigned via the $cdata property, e.g. $x = FFI::new('int'); $x->cdata = 42;

C Data Handles

(PHP 7 >= 7.4.0, PHP 8)

Introduction

FFI\CData objects can be used in a number of ways as a regular PHP data:

  • C data of scalar types can be read and assigned via the $cdata property, e.g. $x = FFI::new('int'); $x->cdata = 42;

  • C struct and union fields can be accessed as regular PHP object property, e.g. $cdata->field

  • C array elements can be accessed as regular PHP array elements, e.g. $cdata[$offset]

  • C arrays can be iterated using foreach statements.

  • C arrays can be used as arguments of count().

  • C pointers can be dereferenced as arrays, e.g. $cdata[0]

  • C pointers can be compared using regular comparison operators (<, <=, ==, !=, >=, >).

  • C pointers can be incremented and decremented using regular +/-/ ++/-- operations, e.g. $cdata += 5

  • C pointers can be subtracted from another using regular - operations.

  • C pointers to functions can be called as a regular PHP closure, e.g. $cdata()

  • Any C data can be duplicated using the clone operator, e.g. $cdata2 = clone $cdata;

  • Any C data can be visualized using var_dump(), print_r(), etc.

  • FFI\CData can now be assigned to structs and fields as of PHP 8.3.0.

Note:

Notable limitations are that FFI\CData instances do not support isset(), empty() and unset(), and that wrapped C structs and unions do not implement Traversable.

Class synopsis

namespace FFI;
final class CData {
}

Changelog

Version Description
8.3.0 FFI\CData can now be assigned to structs and fields.

来源:PHP Documentation Group · 作者与贡献记录 · CC BY 3.0。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
PHP Documentation Group
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。