PHP fun with FFI: Just enough C
Blog post from Upsun
PHP 7.4 introduced Foreign Function Interface (FFI), which allows PHP developers to integrate code from other languages, particularly C, more easily than by writing an extension, though it still requires some effort. The process involves creating a shared library from C code, which can be accessed by PHP during runtime. This post details the creation of a simple C library that calculates the distance between two points using the Pythagorean Theorem, demonstrating the steps of compiling C source files into object files, linking them into an executable, and creating a shared library suitable for PHP FFI. The C program construction process is explained, including the use of header files for defining interfaces and the use of a Makefile to automate the build process, highlighting essential commands and conventions within C programming. The ultimate goal is to prepare the necessary components—specifically the shared object and header files—for integration with PHP's FFI, providing a foundation for PHP developers looking to leverage the performance benefits of C within their PHP applications.