...

/

Using FFI infrastructural methods

Using FFI infrastructural methods

Learn the methods that provide the necessary infrastructure to execute C functions in PHP 8.

FFI extension infrastructural category methods can be thought of as behind-the-scenes components that support the infrastructure needed for C function binding to work properly. The FFI extension is needed if we wish to access C data structures from within a PHP application directly. Thus, if we need to do the equivalent of a PHP unset() statement to release memory or a PHP include() statement to include external program code, the FFI extension infrastructural methods provide the bridge between native C data and PHP.

Summary of FFI Class Infrastructural Methods

FFI Methods

Returns

Notes

addr()

FFI\CData

Creates a pointer to C data.

cast()

FFI\CData

Performs a C language type cast.

free()

void

Releases an FFI pointer. There is no direct equivalent in PHP

memcpy()

void

Copies specified bytes from one FFI\CData instance to another.

memset()

void

Populates a FFI\CData instance with $size bytes consisting of $value.

Let’s first have a look at FFI::addr(), free(), memset(), and memcpy().

Working with FFI::addr(), free(), memset(), and memcpy()

PHP developers often assign a value to a variable by reference. This allows a change in one variable to be automatically reflected in another. The use of references is especially useful when passing parameters to a function or method where we need to return more than a single value. ...