Creating FFI Instances
Learn how to create and utilize various types of FFI instances in PHP 8.
We'll cover the following...
Let’s have a look at creating and using FFI instances.
Creating and using FFI\CType instances
It’s extremely important to note that once the FFI\CType instance has been created, do not simply assign a value to it as if it were a native PHP variable. Doing so would simply overwrite the FFI\CType instance due to the fact that PHP is loosely typed. Instead, to assign a scalar value to an FFI\CType instance, use its cdata property.
The following example creates a $arr C array. The native C array is then populated with values up to its maximum size, after which we use a simple var_dump() to view its contents. We will proceed as follows:
Let’s dive into the code,
Line 3–4: First, we create the array using
FFI::arrayType(). As arguments, we supply aFFI::type()method and dimensions. We then useFFI::new()to create theFFI\Ctypeinstance.Alternatively, ...