FFI::load

(PHP 7 >= 7.4.0, PHP 8)

FFI::loadCarga las declaraciones C desde un archivo de encabezado C

Descripción

public static FFI::load(string $filename): ?FFI

Carga las declaraciones C desde un archivo de encabezado C. Es posible especificar las bibliotecas compartidas que deben ser cargadas, utilizando definiciones especiales FFI_LIB en el archivo de encabezado C cargado.

Parámetros

filename

El nombre de un archivo de encabezado C.

Las directivas del preprocesador C no son soportadas, es decir, #include, #define y las macros CPP no funcionan, excepto en los casos particulares enumerados a continuación.

El archivo de encabezado debería contener una declaración #define para la variable FFI_SCOPE, por ejemplo: #define FFI_SCOPE "MYLIB". Consulte la introducción de la clase para más detalles.

El archivo de encabezado puede contener una declaración #define para la variable FFI_LIB para especificar la biblioteca que expone. Si se trata de una biblioteca del sistema, solo se requiere el nombre del archivo, por ejemplo: #define FFI_LIB "libc.so.6". Si se trata de una biblioteca personalizada, se requiere una ruta relativa, por ejemplo: #define FFI_LIB "./mylib.so".

Valores devueltos

Devuelve el objeto FFI recién creado, o null en caso de fallo.

Historial de cambios

Versión Descripción
8.3.0 FFI::load() ahora está permitido en los scripts de precarga cuando el usuario del sistema actual es el mismo que el definido en la directiva de configuración opcache.preload_user.

Ver también

  • FFI::scope() - Instancia un objeto FFI con las declaraciones C analizadas durante la precarga

add a note

User Contributed Notes 2 notes

up
10
jungmann0 at gmail dot com
4 years ago
Since #include's and #define's other than FFI_LIB and FFI_SCOPE are not supported in the header, you may want to use the C preprocessor to pre-process your headers in order to resolve all #include's and macros.

I use -D"__attribute__(ARGS)=" to remove function attributes as well, which are not supported by FFI either.

This is my script:

echo '#define FFI_SCOPE "YOUR_SCOPE"' > header-ffi.h
echo '#define FFI_LIB "/path/to/your_lib.so"' >> header-ffi.h
cpp -P -C -D"__attribute__(ARGS)=" header_original >> header-ffi.h
up
2
ojrask at gmail dot com
5 years ago
Regarding the `FFI_LIB` constant:

The path given can be either relative, or absolute, or then refer to a library that is in your global libraries path (i.e. where libc.so.6 and friends are).

The absolute path and global lib path work as expected. The relative path does not work from the header file path, but the current working directory instead. So when you create header files for FFI usage, remember that the PHP script can be called anywhere and this means any relative paths in `FFI_LIB` will most probably fail.

Not sure if there is some possibility to make it use paths relative to the script being called, or relative to the header file being loaded. That would make more sense to me at least.

Right now `FFI_LIB` is quite unusable when it comes to making FFI-powered PHP packages that can be installed literally anywhere on a system. `FFI::cdef` works just as well, but `FFI::scope` also seems to rely on `FFI_LIB` meaning it will fail with relative paths as well.

I guess this comes from the C function `dlopen` as is.
To Top