Since: 7.4

class FFI (View source)

FFI class provides access to a simple way to call native functions, access native variables and create/access data structures defined in C language.

Methods

static FFI
cdef(string $code = '', string|null $lib = null)

The method creates a binding on the existing C function.

static FFI|null
load(string $filename)

Instead of embedding of a long C definition into PHP string, and creating FFI through FFI::cdef(), it's possible to separate it into a C header file. Note, that C preprocessor directives (e.g. #define or #ifdef) are not supported. And only a couple of special macros may be used especially for FFI.

static FFI
scope(string $name)

FFI definition parsing and shared library loading may take significant time. It's not useful to do it on each HTTP request in WEB environment. However, it's possible to pre-load FFI definitions and libraries at php startup, and instantiate FFI objects when necessary. Header files may be extended with FFI_SCOPE define (default pre-loading scope is "C"). This name is going to be used as FFI::scope() argument. It's possible to pre-load few files into a single scope.

static CData|null
new(string|CType $type, bool $owned = true, bool $persistent = false)

Method that creates an arbitrary C structure.

static void
free(CData $ptr)

Manually removes previously created "not-owned" data structure.

static CData|null
cast(CType|string $type, CData|int|float|bool|null $ptr)

Casts given $pointer to another C type, specified by C declaration string or FFI\CType object.

static CType|null
type(string $type)

This function creates and returns a FFI\CType object, representng type of the given C type declaration string.

static CType
typeof(CData $ptr)

This function returns the FFI\CType object, representing the type of the given FFI\CData object.

static CType
arrayType(CType $type, array $dimensions)

Constructs a new C array type with elements of $type and dimensions specified by $dimensions.

static CData
addr(CData $ptr)

Returns C pointer to the given C data structure. The pointer is not "owned" and won't be free. Anyway, this is a potentially unsafe operation, because the life-time of the returned pointer may be longer than life-time of the source object, and this may cause dangling pointer dereference (like in regular C).

static int
sizeof(CData|CType $ptr)

Returns size of C data type of the given FFI\CData or FFI\CType.

static int
alignof(CData|CType $ptr)

Returns size of C data type of the given FFI\CData or FFI\CType.

static void
memcpy(CData $to, CData|string $from, int $size)

Copies $size bytes from memory area $source to memory area $target.

static int
memcmp(CData|string $ptr1, CData|string $ptr2, int $size)

Compares $size bytes from memory area $ptr1 and $ptr2.

static void
memset(CData $ptr, int $value, int $size)

Fills the $size bytes of the memory area pointed to by $target with the constant byte $byte.

static string
string(CData $ptr, int|null $size = null)

Creates a PHP string from $size bytes of memory area pointed by $source. If size is omitted, $source must be zero terminated array of C chars.

static bool
isNull(CData $ptr)

Checks whether the FFI\CData is a null pointer.

Details

static FFI cdef(string $code = '', string|null $lib = null)

The method creates a binding on the existing C function.

All variables and functions defined by first arguments are bound to corresponding native symbols in DSO library and then may be accessed as FFI object methods and properties. C types of argument, return value and variables are automatically converted to/from PHP types (if possible). Otherwise, they are wrapped in a special CData proxy object and may be accessed by elements.

Parameters

string $code

The collection of C declarations.

string|null $lib

DSO library.

Return Value

FFI

Exceptions

ParserException

static FFI|null load(string $filename)

Instead of embedding of a long C definition into PHP string, and creating FFI through FFI::cdef(), it's possible to separate it into a C header file. Note, that C preprocessor directives (e.g. #define or #ifdef) are not supported. And only a couple of special macros may be used especially for FFI.

 #define FFI_LIB "libc.so.6"

 int printf(const char *format, ...);

Here, FFI_LIB specifies, that the given library should be loaded.

 $ffi = FFI::load(__DIR__ . "/printf.h");
 $ffi->printf("Hello world!\n");

Parameters

string $filename

Return Value

FFI|null

static FFI scope(string $name)

FFI definition parsing and shared library loading may take significant time. It's not useful to do it on each HTTP request in WEB environment. However, it's possible to pre-load FFI definitions and libraries at php startup, and instantiate FFI objects when necessary. Header files may be extended with FFI_SCOPE define (default pre-loading scope is "C"). This name is going to be used as FFI::scope() argument. It's possible to pre-load few files into a single scope.

 #define FFI_LIB "libc.so.6"
 #define FFI_SCOPE "libc"

 int printf(const char *format, ...);

These files are loaded through the same FFI::load() load function, executed from file loaded by opcache.preload php.ini directive.

 ffi.preload=/etc/php/ffi/printf.h

Finally, FFI::scope() instantiate an FFI object, that implements all C definition from the given scope.

 $ffi = FFI::scope("libc");
 $ffi->printf("Hello world!\n");

Parameters

string $name

Return Value

FFI

static CData|null new(string|CType $type, bool $owned = true, bool $persistent = false)

Method that creates an arbitrary C structure.

Parameters

string|CType $type
bool $owned
bool $persistent

Return Value

CData|null

Exceptions

ParserException

static void free(CData $ptr)

Manually removes previously created "not-owned" data structure.

Parameters

CData $ptr

Return Value

void

static CData|null cast(CType|string $type, CData|int|float|bool|null $ptr)

Casts given $pointer to another C type, specified by C declaration string or FFI\CType object.

This function may be called statically and use only predefined types, or as a method of previously created FFI object. In last case the first argument may reuse all type and tag names defined in FFI::cdef().

Parameters

CType|string $type
CData|int|float|bool|null $ptr

Return Value

CData|null

static CType|null type(string $type)

This function creates and returns a FFI\CType object, representng type of the given C type declaration string.

FFI::type() may be called statically and use only predefined types, or as a method of previously created FFI object. In last case the first argument may reuse all type and tag names defined in FFI::cdef().

Parameters

string $type

Return Value

CType|null

static CType typeof(CData $ptr)

This function returns the FFI\CType object, representing the type of the given FFI\CData object.

Parameters

CData $ptr

Return Value

CType

static CType arrayType(CType $type, array $dimensions)

Constructs a new C array type with elements of $type and dimensions specified by $dimensions.

Parameters

CType $type
array $dimensions

Return Value

CType

static CData addr(CData $ptr)

Returns C pointer to the given C data structure. The pointer is not "owned" and won't be free. Anyway, this is a potentially unsafe operation, because the life-time of the returned pointer may be longer than life-time of the source object, and this may cause dangling pointer dereference (like in regular C).

Parameters

CData $ptr

Return Value

CData

static int sizeof(CData|CType $ptr)

Returns size of C data type of the given FFI\CData or FFI\CType.

Parameters

CData|CType $ptr

Return Value

int

static int alignof(CData|CType $ptr)

Returns size of C data type of the given FFI\CData or FFI\CType.

Parameters

CData|CType $ptr

Return Value

int

static void memcpy(CData $to, CData|string $from, int $size)

Copies $size bytes from memory area $source to memory area $target.

$source may be any native data structure (FFI\CData) or PHP string.

Parameters

CData $to
CData|string $from
int $size

Return Value

void

static int memcmp(CData|string $ptr1, CData|string $ptr2, int $size)

Compares $size bytes from memory area $ptr1 and $ptr2.

Parameters

CData|string $ptr1
CData|string $ptr2
int $size

Return Value

int

static void memset(CData $ptr, int $value, int $size)

Fills the $size bytes of the memory area pointed to by $target with the constant byte $byte.

Parameters

CData $ptr
int $value
int $size

Return Value

void

static string string(CData $ptr, int|null $size = null)

Creates a PHP string from $size bytes of memory area pointed by $source. If size is omitted, $source must be zero terminated array of C chars.

Parameters

CData $ptr
int|null $size

Return Value

string

static bool isNull(CData $ptr)

Checks whether the FFI\CData is a null pointer.

Parameters

CData $ptr

Return Value

bool