FFI::cast

(PHP 7 >= 7.4.0)

FFI::castPerforms a C type cast

Descrierea

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

FFI::cast() creates a new FFI\CData object, that references the same C data structure, but is associated with a different type. The resulting object does not own the C data, and the source ptr must survive the result. The C type may be specified as a string with any valid C type declaration or as FFI\CType object, created before. If this method is called statically, it must only use predefined C type names (e.g. int, char, etc.); if the method is called as instance method, any type declared for the instance is allowed.

Parametri

type

A valid C declaration as string, or an instance of FFI\CType which has already been created.

ptr

The handle of the pointer to a C data structure.

Valorile întoarse

Returns the freshly created FFI\CData object.

add a note add a note

User Contributed Notes 1 note

up
-1
Yaner
2 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