r/PHPhelp • u/WhatForIamHere • Apr 28 '25
Solved FFI cdef doesn't accept a constant as a C array size
Hi All
I have the next piece of code in .h file:
const int ARRAY_SIZE 100
int array[ARRAY_SIZE];
PHP FFI is producing the next error until I call cdef():
PHP Fatal error: Uncaught FFI\ParserException: Unsupported array index type at line ...
The same if I'm trying to use #define ARRAY_SIZE 100. Everything is working fine if I change to
int array[100];
How to struggle with it? Does FFI cdef predefined some cpp defines for enclosure such pieces of code to #if .. #end pairs? Or, could anyone please provide any other appropriate solution for using such code in FFI?
Thanks in advance!
2
u/WhatForIamHere Apr 28 '25
I did find the solution. Just wrapped out constants to enum ))
enum _dummy {ARRAY_SIZE=100};int array[ARRAY_SIZE];Now it works fine for C and FFI simultaneously.
2
u/innosu_ Apr 28 '25
FFI cdef doesn't evaluate anything. Not preprocessor. Not constant.
2
u/obstreperous_troll Apr 28 '25
There's hardly any PHP packages in the wild that use FFI. Now you know why.
1
u/eurosat7 Apr 28 '25 edited Apr 28 '25
Ffi? I have no idea. TRY:
Or maybe you find it in a superglobal like $_ENV