PHPackages                             toknot/ffi-extend - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. toknot/ffi-extend

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

toknot/ffi-extend
=================

call PHP C API.

0.1.5(5y ago)9149[2 issues](https://github.com/chopins/ffi-ext/issues)2BSD-3-ClausePHPPHP &gt;=7.4.0

Since Mar 12Pushed 5y ago1 watchersCompare

[ Source](https://github.com/chopins/ffi-ext)[ Packagist](https://packagist.org/packages/toknot/ffi-extend)[ Docs](http://toknot.com)[ RSS](/packages/toknot-ffi-extend/feed)WikiDiscussions master Synced today

READMEChangelog (9)DependenciesVersions (7)Used By (2)

[中文](#php-ffi-%E5%A2%9E%E5%BC%BA%E5%8A%9F%E8%83%BD)

php FFI Extend
==============

[](#php-ffi-extend)

Call PHP C API

php DLL(php7.dll,php7ts.dll) find order, if file exists loading:

- like unix os, Usually does not require additional loading
- in windows, find order:
    1. if undefine constant `PHP_DLL_FILE_PATH`, will find environment variable `PHP_DLL_FILE_PATH`
    2. find user constant `PHP_DLL_FILE_PATH`
    3. find directory of predefined constant `PHP_BINARY` (physical location, not `PHP_BINDIR`)
    4. find php parent directory of ext directory (physical location, not `PHP_EXTENSION_DIR`)
    5. find `PHP_BINDIR` directory
    6. find `PHP_LIBDIR` directory

**Note: constant `PHP_DLL_FILE_PATH` work for like unix OS**

\*\*constant `PHP_FFI_EXTEND_APPEND_CDEF` is append cdef code

Reference
=========

[](#reference)

### class `Toknot\FFIExtend` of methods

[](#class-toknotffiextend-of-methods)

方法描述1. `__construct()`2. `phpffi() : ffi`get ffi3. `emalloc($size) : CData`emalloc memory, and set 04. `castSameType(FFI $ffi, &$arg)`cast to same type5. `zvalValue(FFI\CData $zval) : CData`get value of php C zval struct6. `zval($var): CData`get C zval(CData) from php variable($var, php string,int ...)7. `ZSTR_VAL(CData $str) : CData`php C `ZSTR_VAL` macro8. `ZSTR_LEN(CData $str) : int`php C `ZSTR_LEN` macro9. `getCTypeName(CType $type):string`get C type name10. `Z_TYPE(CData $type) : int`php C `Z_TYPE` macro11. `getZStr(CData $str) : string`get string from string zval12. `Z_OBJ_P(CData $obj)`php C `Z_OBJ_P` macro, `$obj` must be pointer13. `isNull($v) : bool`check `$v` whether is `NULL` or C `NULL`14. `zend_hash_find_ptr(CData $zendArrayPtr, CData $name, string $type): CData`find a value from php C hash array, and cast to `$type`15. `zend_hash_num_elements(CData $array) : int`get number of php C HashTable16. `hasCFunc(FFI $ffi, string $name) : bool`check given `$name` of function whether in given FFI `$ffi`17. `hasCVariable(FFI $ffi, string $name) : bool`check given `$name` of variable whether in given FFI `$ffi`18. `hasCType(FFI $ffi, string $name) : bool`check given `$name` of c type whether in given FFI `$ffi`19. `Z_PTR_P(CData $zval) : CData`php C `Z_PTR_P` macro, `$zval` must be pointer20. `ZEND_FFI_TYPE(CData $t) : CData`php FFI of `ZEND_FFI_TYPE` macro21. `castAllSameType(FFI $ffi, array &$args)`cast array of args to same type22. `iteratorZendArray(CData $hashTable, callable $callable)`iterator a zend HashTable, `$hashTable` must be pointer, the `$callable` smailer to `function callback($key, $value)`23. `argsPtr(int $argc, array $argv): CData`php `$argv` array to C `char**`24. `strToCharPtr(string $string): CData`php string to C `char*`25. `strToCharArr(string $string): CData`php string to C `char[]`26. `is64(): bool`check os whether is 64bit (whether is LP64)### class `Toknot\ReflectionCFunction` of methods

[](#class-toknotreflectioncfunction-of-methods)

方法描述1. `__construct(FFI $ffi, string $name)`Reflection FFI `$ffi` of C function `$name`2. `getName()`get function name3. `getClosure() : Closure`4. `isVariadic() : bool`5. `getNumberOfParameters() : int`get number of parameters6. `getReturnType(): string`get return type7. `getParameters(): array`get parameters type listPHP FFI 增强功能
============

[](#php-ffi-增强功能)

PHP DDL(php7.dll,php7ts.dll，动态库)查找顺序，如果文件存在：

- 对于类UNIX系统，通常是不需要指定加载。通常当PHP以其他程序的模块方式安装时需要指定
- 对于windows
    1. 如果未定义常量`PHP_DLL_FILE_PATH`，将会使用环境变量 `PHP_DLL_FILE_PATH`定义的路径
    2. 根据常量 `PHP_DLL_FILE_PATH` 指定路径查找,（实际位置，非`PHP_BINDIR`)
    3. 根据PHP预定义常量`PHP_BINARY` 指定的路径查找 (实际位置，非`PHP_EXTENSION_DIR`)
    4. 在PHP扩展所在文件夹的上一层文件夹下查找
    5. 在`PHP_BINDIR`目录查找
    6. 在`PHP_LIBDIR`目录查找

**注意: 常量 `PHP_DLL_FILE_PATH` 在类UNIX系统下依然有效**

类方法参考列表
=======

[](#类方法参考列表)

### 类 `Toknot\FFIExtend` 的方法

[](#类-toknotffiextend-的方法)

方法描述1. `__construct()`2. `phpffi() : ffi`获得PHP C 接口的 FFI 类对象3. `emalloc($size) : CData`分配内存，并设置为04. `castSameType(FFI $ffi, &$arg)`转换任意其他FFI对象的C数据到指定FFI对象的同结构名字的C数据5. `zvalValue(FFI\CData $zval) : CData`get value of php C zval struct6. `zval($var): CData`get C zval(CData) from php variable($var, php string,int ...)7. `ZSTR_VAL(CData $str) : CData`php C `ZSTR_VAL` macro8. `ZSTR_LEN(CData $str) : int`php C `ZSTR_LEN` macro9. `getCTypeName(CType $type):string`获取指定CType的结构名字10. `Z_TYPE(CData $type) : int`php C `Z_TYPE` macro11. `getZStr(CData $str) : string`get string from string zval12. `Z_OBJ_P(CData $obj)`php C `Z_OBJ_P` macro, `$obj` must be pointer13. `isNull($v) : bool`检查 `$v` 是 PHP `NULL` 或 C `NULL`14. `zend_hash_find_ptr(CData $zendArrayPtr, CData $name, string $type): CData`从 PHP hashTable 中查找指定元数的值，并转换成指定类型15. `zend_hash_num_elements(CData $array) : int`获取 php HashTable 元素的个数16. `hasCFunc(FFI $ffi, string $name) : bool`检测指定名字的函数是否存在与指定的FFI对象中17. `hasCVariable(FFI $ffi, string $name) : bool`在FFI对象中，检查指定函数名字是否是可变函数18. `hasCType(FFI $ffi, string $name) : bool`在FFI对象中，检查指定的类型名是否存在19. `Z_PTR_P(CData $zval) : CData`php C `Z_PTR_P` macro, `$zval` must be pointer20. `ZEND_FFI_TYPE(CData $t) : CData`php FFI of `ZEND_FFI_TYPE` macro21. `castAllSameType(FFI $ffi, array &$args)`批量转换到指定FFI的同名数据结构22. `iteratorZendArray(CData $hashTable, callable $callable)`迭代一个Zend HashTable,`$hashTable`必须数指针，`$callable`原型必须类似`function callback($key, $value)`23. `argsPtr(int $argc, array $argv): CData`将PHP `$argv` 数组转换成C `char**`24. `strToCharPtr(string $string): CData`将 PHP 字符串转换成C `char*`25. `strToCharArr(string $string): CData`将 PHP 字符串转换成C `char[]`26. `is64(): bool`检查系统是否是64位(是否是64位数据模型)### 类 `Toknot\ReflectionCFunction` 的方法

[](#类-toknotreflectioncfunction-的方法)

方法描述1. `__construct(FFI $ffi, string $name)`反射指定 FFI `$ffi` 的 C 函数 `$name`2. `getName()`获取函数名3. `getClosure() : Closure`返回函数的闭包4. `isVariadic() : bool`是否是可变函数5. `getNumberOfParameters() : int`获取参数个数6. `getReturnType(): string`获取返回类型7. `getParameters(): array`获取参数类型列表

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance16

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~49 days

Recently: every ~15 days

Total

6

Last Release

2006d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/3683f29364f9fba39ebd7e90377b67c28ca2497e01cefc19ebb40df6cda72ddb?d=identicon)[szopen](/maintainers/szopen)

---

Top Contributors

[![chopins](https://avatars.githubusercontent.com/u/1539465?v=4)](https://github.com/chopins "chopins (60 commits)")

---

Tags

cffiphpphpextendextffic

### Embed Badge

![Health badge](/badges/toknot-ffi-extend/health.svg)

```
[![Health](https://phpackages.com/badges/toknot-ffi-extend/health.svg)](https://phpackages.com/packages/toknot-ffi-extend)
```

###  Alternatives

[imanghafoori/laravel-anypass

A minimal yet powerful package to help you in development.

21421.6k](/packages/imanghafoori-laravel-anypass)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
