外部函数接口
简介
此扩展允许在纯 PHP 中加载共享库(.DLL 或 .so)、调用 C 函数、访问 C 数据结构,而无需深入了解 Zend 扩展 API,也无需学习第三方“中间”语言。公共 API 是使用包含几个静态方法(有几个可以动态调用)和对象重载方法(执行与 C 数据的实际交互)的 FFI 类实现的。
警告
FFI 有危险,因为它允许在系统上进行底层交互。FFI 扩展应该仅供有 C 和使用 C API 经验的开发者使用。为了最大限度降低风险,ffi.enable php.ini 指令会限制 FFI API 的使用。
注意:
FFI 扩展不会提供讨厌的过时 PHP 扩展 API,它仅仅是提供了用于 C 函数和数据结构的特殊接口。
小技巧
目前,访问 FFI 数据结构比访问原生 PHP 数组和对象慢很多(大约 2 倍)。 因此使用 FFI 扩展来提高速度没有意义;减少内存消耗可能有意义。
- 安装/配置
- 示例
- FFI — C 代码和数据的主接口
- FFI::addr — Creates an unmanaged pointer to C data
- FFI::alignof — Gets the alignment
- FFI::arrayType — Dynamically constructs a new C array type
- FFI::cast — Performs a C type cast
- FFI::cdef — Creates a new FFI object
- FFI::free — Releases an unmanaged data structure
- FFI::isNull — Checks whether a FFI\CData is a null pointer
- FFI::load — Loads C declarations from a C header file
- FFI::memcmp — Compares memory areas
- FFI::memcpy — Copies one memory area to another
- FFI::memset — Fills a memory area
- FFI::new — Creates a C data structure
- FFI::scope — Instantiates an FFI object with C declarations parsed during preloading
- FFI::sizeof — Gets the size of C data or types
- FFI::string — Creates a PHP string from a memory area
- FFI::type — Creates an FFI\CType object from a C declaration
- FFI::typeof — Gets the FFI\CType of FFI\CData
- FFI\CData — C Data Handles
- FFI\CType — C Type Handles
- FFI\CType::getAlignment — Description
- FFI\CType::getArrayElementType — Description
- FFI\CType::getArrayLength — Description
- FFI\CType::getAttributes — Description
- FFI\CType::getEnumKind — Description
- FFI\CType::getFuncABI — Description
- FFI\CType::getFuncParameterCount — Retrieve the count of parameters of a function type
- FFI\CType::getFuncParameterType — Retrieve the type of a function parameter
- FFI\CType::getFuncReturnType — Description
- FFI\CType::getKind — Description
- FFI\CType::getName — Description
- FFI\CType::getPointerType — Description
- FFI\CType::getSize — Description
- FFI\CType::getStructFieldNames — Description
- FFI\CType::getStructFieldOffset — Description
- FFI\CType::getStructFieldType — Description
- FFI\Exception — FFI Exceptions
- FFI\ParserException — FFI Parser Exceptions
来源:PHP Documentation Group · 作者与贡献记录 · CC BY 3.0。离线整理了版式、链接和媒体显示。
