PHP 8.5.0 RC 5 available for testing

FFI::new

(PHP 7 >= 7.4.0, PHP 8)

FFI::newC のデータ構造を作成する

説明

public FFI::new(FFI\CType|string $type, bool $owned = true, bool $persistent = false): ?FFI\CData

与えられた C の型を持つネイティブデータ構造を作成します。 このインスタンスで宣言された任意の型が使えます。

パラメータ

type

type は、有効な C の宣言を表す string か、 作成済みの FFI\CType のインスタンスです。

owned

所有された (マネージドな) データを作成するか、アンマネージドなデータを作成するか。 マネージドデータは返される FFI\CData オブジェクトと共に生存し、 そのオブジェクトへの最後の参照が PHP の通常のリファレンスカウントや GC によって解放されたときに解放されます。 アンマネージドなデータは、不要になったら FFI::free() を呼んで解放すべきです。

persistent

この C のデータ構造をシステムヒープ上に永続的に確保するか (malloc() を使用)、 PHP のリクエストヒープ上に確保するか (emalloc() を使用)。

戻り値

新しく作成された FFI\CData オブジェクトを返します。 失敗時には null を返します。

変更履歴

バージョン 説明
8.3.0 FFI::new() を static メソッドとして呼び出すのは非推奨となりました。

add a note

User Contributed Notes 1 note

up
0
baminazad at cs dot stonybrook dot edu
5 years ago
Let's assume we have a C struct:
typedef struct _Z3_ast *Z3_ast;

and we want to create an array: 
Z3_ast args[2];

and assign values:
args[1] = x;
args[1] = y;

The PHP FFI equivalent would be:
<?php
$ffi = FFI::cdef(...
// Create Z3_ast[2] type
$arg_type = FFI::arrayType($ffi->type('Z3_ast'), [2]);
// Create array of type Z3_ast[2]
$args = FFI::new($arg_type);
// Populate the array
$args[0] = $x;
$args[1] = $y;
?>
To Top