Search⌘ K

Learning Where to Use FFI

Explore the practical use of PHP 8's Foreign Function Interface (FFI) to directly call C language functions. Understand the strengths and limitations of FFI, its role in rapid extension prototyping, and scenarios where it facilitates direct integration with proprietary C code or complex C libraries.

The potential for importing C libraries directly into PHP is truly staggering. One of the PHP core developers actually used the FFI extension to bind PHP to the C-language TensorFlow machine learning platform!

As we show in this section, the FFI extension is not a magic solution for all of our needs. This section discusses the main strengths and weaknesses of the FFI extension, as well as gives us guidelines for its use. A myth we debunk in this section is that making direct C-language calls using the FFI extension speeds up PHP 8 program execution. First, let’s have a look at what took so long to get the FFI extension into PHP.

Adopting FFI into PHP

The first FFI extension was actually introduced for PHP 5 on the PECL website in January 2004 by PHP core developers Wez Furlong and Ilia Alshanetsky. The project never passed its Alpha stage, however, and development was dropped within a month. ...