PHP8 C 代碼和數(shù)據(jù)的主接口

2023-08-31 17:47 更新

(PHP 7 >= 7.4.0, PHP 8)

簡(jiǎn)介

通過(guò)工廠方法 FFI::cdef()、FFI::load() 或 FFI::scope() 創(chuàng)建該類的對(duì)象。定義的 C 變量作為有效的 FFI 實(shí)例屬性,定義的 C 函數(shù)作為有效的 FFI 實(shí)例方法。聲明的 C 類型可以用于 FFI::new() 和 FFI::type() 創(chuàng)建新的 C 數(shù)據(jù)結(jié)構(gòu)。

FFI 定義解析和共享庫(kù)加載可能需要較長(zhǎng)時(shí)間。在 Web 環(huán)境中,每個(gè) HTTP 請(qǐng)求都進(jìn)行這些操作是沒(méi)有意義的。然而,在 PHP 啟動(dòng)時(shí)預(yù)加載 FFI 定義和庫(kù),并在需要時(shí)實(shí)例化 FFI 對(duì)象是可能的。header 文件可以使用特殊的 FFI_SCOPE 定義進(jìn)行擴(kuò)展(例如 #define FFI_SCOPE "foo"”"),然后在預(yù)加載期間由 FFI::load() 加載。這將創(chuàng)建持久綁定,將通過(guò) FFI::scope() 在所有后續(xù)請(qǐng)求中可用。有關(guān)詳細(xì)信息,請(qǐng)參閱完整的 PHP/FFI/preloading 示例。

可以將多個(gè) C header 文件預(yù)加載到同一作用域中。

類摘要

final class FFI {
/* 常量 */
public const int __BIGGEST_ALIGNMENT__;
/* 方法 */
public static addr(FFI\CData &$ptr): FFI\CData
public static alignof(FFI\CData|FFI\CType &$ptr): int
public static arrayType(FFI\CType $type, array $dimensions): FFI\CType
public static cast(FFI\CType|string $type, FFI\CData|int|float|bool|null &$ptr): ?FFI\CData
public cast(FFI\CType|string $type, FFI\CData|int|float|bool|null &$ptr): ?FFI\CData
public static cdef(string $code = "", ?string $lib = null): FFI
public static free(FFI\CData &$ptr): void
public static isNull(FFI\CData &$ptr): bool
public static load(string $filename): ?FFI
public static memcmp(string|FFI\CData &$ptr1, string|FFI\CData &$ptr2, int $size): int
public static memcpy(FFI\CData &$to, FFI\CData|string &$from, int $size): void
public static memset(FFI\CData &$ptr, int $value, int $size): void
public static new(FFI\CType|string $type, bool $owned = true, bool $persistent = false): ?FFI\CData
public new(FFI\CType|string $type, bool $owned = true, bool $persistent = false): ?FFI\CData
public static scope(string $name): FFI
public static sizeof(FFI\CData|FFI\CType &$ptr): int
public static string(FFI\CData &$ptr, ?int $size = null): string
public static type(string $type): ?FFI\CType
public type(string $type): ?FFI\CType
public static typeof(FFI\CData &$ptr): FFI\CType
}

預(yù)定義常量

FFI::__BIGGEST_ALIGNMENT__

目錄

  • 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


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)