update page now
Laravel Live Japan

FFI::cast

(PHP 7 >= 7.4.0, PHP 8)

FFI::castC の型キャストを実行する

説明

public FFI::cast(FFI\CType|string $type, FFI\CData|int|float|bool|null &$ptr): ?FFI\CData

FFI::cast() は、同じ C のデータ構造を参照するものの別の型が紐付けられた FFI\CData オブジェクトを新しく作成します。 返却されるオブジェクトはその C のデータを所有しません。元の ptr は その返り値よりも長く生存する必要があります。 C の型は、有効な C の型宣言を表す string として指定するか、 以前作成した FFI\CType オブジェクトとして指定します。 このインスタンスで宣言された任意の型が使えます。

パラメータ

type

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

ptr

C のデータ構造へのポインターのハンドル。

戻り値

新しく作成された FFI\CData オブジェクトを返します。

変更履歴

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

add a note

User Contributed Notes 1 note

up
-4
Yaner
3 years ago
For example, stdlib.h headfile defines a function called "system()" in Linux:  extern int system (const char *__command) __wur;
And we can call it using FFI extension:

<?php
    $ffi_obj = FFI::cdef('int system(char *command);')
    $ffi_obj->system('whoami');
?>

Then execute the php script as if we were calling the real C  `system()`:

$ whoami
> root
$ php demo.php
> root
To Top