Foreign Function Interface

目次

Main interface to C code and data

はじめに

Objects of this class are created by the factory methods FFI::cdef, FFI::load or FFI::scope. Defined C variables are made available as properties of the FFI instance, and defined C functions are made available as methods of the FFI instance. Declared C types can be used to create new C data structures using FFI::new and FFI::type.

FFI definition parsing and shared library loading may take significant time. It is not useful to do it on each HTTP request in a Web environment. However, it is possible to preload FFI definitions and libraries at PHP startup, and to instantiate FFI objects when necessary. Header files may be extended with special FFI_SCOPE defines (e.g. #define FFI_SCOPE "foo"; the default scope is "C") and then loaded by FFI::load during preloading. This leads to the creation of a persistent binding, that will be available to all the following requests through FFI::scope. Refer to the complete PHP/FFI/preloading example for details.

It is possible to preload more than one C header file into the same scope.

クラス概要

final FFI
/* 定数 */
public const int FFI::__BIGGEST_ALIGNMENT__;
/* メソッド */
public static FFI\CData addr(FFI\CData &$ptr)
public static int alignof(FFI\CDataFFI\CType &$ptr)
public static FFI\CType arrayType(FFI\CType $type, array $dimensions)
public FFI\CDatanull cast(FFI\CTypestring $type, FFI\CDataintfloatboolnull &$ptr)
public static FFI cdef(string $code = "", stringnull $lib = null)
public static void free(FFI\CData &$ptr)
public static bool isNull(FFI\CData &$ptr)
public static FFInull load(string $filename)
public static int memcmp(stringFFI\CData &$ptr1, stringFFI\CData &$ptr2, int $size)
public static void memcpy(FFI\CData &$to, FFI\CDatastring &$from, int $size)
public static void memset(FFI\CData &$ptr, int $value, int $size)
public FFI\CDatanull new(FFI\CTypestring $type, bool $owned = true, bool $persistent = false)
public static FFI scope(string $name)
public static int sizeof(FFI\CDataFFI\CType &$ptr)
public static string string(FFI\CData &$ptr, intnull $size = null)
public FFI\CTypenull type(string $type)
public static FFI\CType typeof(FFI\CData &$ptr)

定義済み定数

FFI::__BIGGEST_ALIGNMENT__

C Data Handles

はじめに

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.

注意: 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.

クラス概要

final FFI\CData

変更履歴

バージョン 説明
8.3.0 FFI\CData can now be assigned to structs and fields.

C Type Handles

はじめに

クラス概要

final FFI\CType
/* 定数 */
public const int FFI\CType::TYPE_VOID;
public const int FFI\CType::TYPE_FLOAT;
public const int FFI\CType::TYPE_DOUBLE;
public const int FFI\CType::TYPE_LONGDOUBLE;
public const int FFI\CType::TYPE_UINT8;
public const int FFI\CType::TYPE_SINT8;
public const int FFI\CType::TYPE_UINT16;
public const int FFI\CType::TYPE_SINT16;
public const int FFI\CType::TYPE_UINT32;
public const int FFI\CType::TYPE_SINT32;
public const int FFI\CType::TYPE_UINT64;
public const int FFI\CType::TYPE_SINT64;
public const int FFI\CType::TYPE_ENUM;
public const int FFI\CType::TYPE_BOOL;
public const int FFI\CType::TYPE_CHAR;
public const int FFI\CType::TYPE_POINTER;
public const int FFI\CType::TYPE_FUNC;
public const int FFI\CType::TYPE_ARRAY;
public const int FFI\CType::TYPE_STRUCT;
public const int FFI\CType::ATTR_CONST;
public const int FFI\CType::ATTR_INCOMPLETE_TAG;
public const int FFI\CType::ATTR_VARIADIC;
public const int FFI\CType::ATTR_INCOMPLETE_ARRAY;
public const int FFI\CType::ATTR_VLA;
public const int FFI\CType::ATTR_UNION;
public const int FFI\CType::ATTR_PACKED;
public const int FFI\CType::ATTR_MS_STRUCT;
public const int FFI\CType::ATTR_GCC_STRUCT;
public const int FFI\CType::ABI_DEFAULT;
public const int FFI\CType::ABI_CDECL;
public const int FFI\CType::ABI_FASTCALL;
public const int FFI\CType::ABI_THISCALL;
public const int FFI\CType::ABI_STDCALL;
public const int FFI\CType::ABI_PASCAL;
public const int FFI\CType::ABI_REGISTER;
public const int FFI\CType::ABI_MS;
public const int FFI\CType::ABI_SYSV;
public const int FFI\CType::ABI_VECTORCALL;
/* メソッド */
public int getAlignment()
public FFI\CType getArrayElementType()
public int getArrayLength()
public int getAttributes()
public int getEnumKind()
public int getFuncABI()
public int getFuncParameterCount()
public FFI\CType getFuncParameterType(int $index)
public FFI\CType getFuncReturnType()
public int getKind()
public string getName()
public FFI\CType getPointerType()
public int getSize()
public array getStructFieldNames()
public int getStructFieldOffset(string $name)
public FFI\CType getStructFieldType(string $name)

定義済み定数

FFI\CType::TYPE_VOID

FFI\CType::TYPE_FLOAT

FFI\CType::TYPE_DOUBLE

FFI\CType::TYPE_LONGDOUBLE

FFI\CType::TYPE_UINT8

FFI\CType::TYPE_SINT8

FFI\CType::TYPE_UINT16

FFI\CType::TYPE_SINT16

FFI\CType::TYPE_UINT32

FFI\CType::TYPE_SINT32

FFI\CType::TYPE_UINT64

FFI\CType::TYPE_SINT64

FFI\CType::TYPE_ENUM

FFI\CType::TYPE_BOOL

FFI\CType::TYPE_CHAR

FFI\CType::TYPE_POINTER

FFI\CType::TYPE_FUNC

FFI\CType::TYPE_ARRAY

FFI\CType::TYPE_STRUCT

FFI\CType::ATTR_CONST

FFI\CType::ATTR_INCOMPLETE_TAG

FFI\CType::ATTR_VARIADIC

FFI\CType::ATTR_INCOMPLETE_ARRAY

FFI\CType::ATTR_VLA

FFI\CType::ATTR_UNION

FFI\CType::ATTR_PACKED

FFI\CType::ATTR_MS_STRUCT

FFI\CType::ATTR_GCC_STRUCT

FFI\CType::ABI_DEFAULT

FFI\CType::ABI_CDECL

FFI\CType::ABI_FASTCALL

FFI\CType::ABI_THISCALL

FFI\CType::ABI_STDCALL

FFI\CType::ABI_PASCAL

FFI\CType::ABI_REGISTER

FFI\CType::ABI_MS

FFI\CType::ABI_SYSV

FFI\CType::ABI_VECTORCALL

FFI Exceptions

はじめに

クラス概要

FFI\Exception
extends Error
/* 継承したプロパティ */
protected string $message = "";
private string $string = "";
protected int $code;
protected string $file = "";
protected int $line;
private array $trace = [];
private Throwablenull $previous = null;
/* 継承したメソッド */
public __construct(string $message = "", int $code = 0, Throwablenull $previous = null)
final public string getMessage()
final public Throwablenull getPrevious()
final public int getCode()
final public string getFile()
final public int getLine()
final public array getTrace()
final public string getTraceAsString()
public string __toString()
private void __clone()

FFI Parser Exceptions

はじめに

クラス概要

final FFI\ParserException
extends FFI\Exception
/* 継承したプロパティ */
protected string $message = "";
private string $string = "";
protected int $code;
protected string $file = "";
protected int $line;
private array $trace = [];
private Throwablenull $previous = null;
/* 継承したメソッド */
public Error::__construct(string $message = "", int $code = 0, Throwablenull $previous = null)
final public string Error::getMessage()
final public Throwablenull Error::getPrevious()
final public int Error::getCode()
final public string Error::getFile()
final public int Error::getLine()
final public array Error::getTrace()
final public string Error::getTraceAsString()
public string Error::__toString()
private void Error::__clone()