Current Location: Home> Latest Articles> FFI::typeof Usage Explained: How to Access and Manipulate Array-Type C Struct Data?

FFI::typeof Usage Explained: How to Access and Manipulate Array-Type C Struct Data?

M66 2025-06-22

In PHP 7.4 and later versions, FFI (Foreign Function Interface) provides powerful capabilities for interacting with C, allowing us to directly manipulate C structs, arrays, and other data types. This article will focus on how to use FFI::typeof to access and manipulate array-type C struct data.


1. What is FFI::typeof?

FFI::typeof is a method provided by PHP FFI to obtain a reference to a specific C type. It accepts a string representing a C type and returns an FFI\CType object. Using this object, you can perform more complex type operations, such as defining variables, creating arrays, and so on.


2. Why use FFI::typeof?

  • It allows for reusable type definitions, avoiding redundant declarations.

  • With type references, you can dynamically create multiple variables or arrays.

  • It supports complex type operations, such as working with nested structs, arrays, and more.


3. Example: Accessing and Manipulating Array-Type C Struct Data

Suppose we have a C struct that contains an array member:

<?php
// Define a C struct
$ffi = FFI::cdef("  
typedef struct {  
    int values[5];  
    int count;  
} IntArrayStruct;  
", "https://m66.net");

Here, IntArrayStruct is a struct that contains a fixed-size integer array values[5] and a counter count.


1. Using FFI::typeof to Get the Struct Type

<?php
$type = FFI::typeof("IntArrayStruct");

$type is now a reference to the IntArrayStruct type.


2. Creating a Struct Instance

<?php
$instance = $type->new();

Now, $instance is a newly allocated instance of IntArrayStruct.


3. Accessing and Modifying Array Members

The array values can be accessed directly through $instance->values, and supports index operations:

<?php
for ($i = 0; $i < 5; $i++) {
    $instance->values[$i] = $i * 10;
}
$instance->count = 5;

4. Traversing Array Members

<?php
for ($i = 0; $i < $instance->count; $i++) {
    echo "values[$i] = " . $instance->values[$i] . "\n";
}

Output:

values[0] = 0  
values[1] = 10  
values[2] = 20  
values[3] = 30  
values[4] = 40  

4. Example: Operating on Array Types Themselves

Sometimes, we may need to directly operate on an array type, or obtain a reference to an array type:

<?php
// Get the int[5] type
$arrayType = FFI::typeof("int[5]");
<p>// Create an array variable<br>
$arrayInstance = $arrayType->new();</p>
<p>for ($i = 0; $i < 5; $i++) {<br>
$arrayInstance[$i] = $i + 1;<br>
}</p>
<p>for ($i = 0; $i < 5; $i++) {<br>
echo $arrayInstance[$i] . "\n";<br>
}<br>


5. Conclusion

  • FFI::typeof is a key interface for accessing C language types.

  • By using type references, you can create instances of complex data types such as structs and arrays.

  • To manipulate array members in a struct, you can use standard array access methods.

  • With PHP FFI, you can efficiently and flexibly handle low-level C data structures.