PHP Forward Function Interface
January 16, 2020

What Is PHP Foreign Function Interface (FFI)?

PHP Development

What Is PHP Foreign Function Interface?

Introduced in PHP 7.4, the PHP Foreign Function Interface is truly groundbreaking. The function allows for the inclusion of external shared libraries directly from PHP userland code. PHP FFI (PHP Foreign Function Interface) opens up a new way for developers to write php extensions and bindings to C libraries in pure PHP. FFI was made available in Python and LuaJIT and made those languages very useful for fast prototyping.

Why Is PHP Foreign Function Interface Important?

PHP Foreign Function Interface has met multiple needs and has potential to continue to gain value. It has begun to open doors previously closed to PHP developers. 

Here are a few of the reasons PHP FFI brings value:
  • Rapid prototyping for libraries for which no extensions are yet available in PHP (e.g. Tensorflow notoriously difficult to load in PHP):
    • Dmitry Stogov, one of the main php core contributors, a Zend by Perforce employee, and the author of PHP FFI wrote a POC for FFI using Tensorflow.
    • Remi Collet started testing a Redis binding.
    • Many other extensions that are not commonly used in PHP in the ML space (BERT, TensorFlow, etc.), proprietary software libraries.
  • Provides solutions for shops that have no C expertise and need to interface with some shared libraries (proprietary software). With enough PHP expertise, they can now develop bindings in house. 
  • An organization with a particular business function that is compute heavy could develop a piece of code in C to run CPU intensive workloads faster.
  • PHP FFI has interesting applications with PHP Async (e.g. Swoole) and with libraries that have long pre-loading times (e.g. Machine Learning models). 

What Is the Current Status of the Extension? 

The FFI extension was first introduced in PHP 7.4 and is still considered experimental. It is likely that there will be more investments in features and performance improvements over the course of 2020. 

Will It Ever Replace PHP Extensions?

It’s too early to tell. Many PHP extensions are a lot more than just a binding to a C library (e.g. PDO); those it is unlikely to be replaced. Although, simpler extensions might replace it if performance is on par or better. However, at the moment of writing, FFI is slower than a pure C/C++ implementation of an extension in virtually every case.

How Does It Work?

It’s fairly simple to get FFI to work. You will need a minimal header file for the library that you are planning on binding with. The header file (a .h file in C) defines the interfaces that will be available in PHP along with the data types. After that, you go ahead and instantiate FFI with that header file and/or the library that you’re trying to load. Lastly, you then prepare the data structures if needed, and call functions from the library that you are binding through the FFI instance as if they were methods of the FFI object. 

See these links for help: 

Conclusion

PHP FFI is an easy-to-use function that the community is expectant to see do well. Very useful for quick prototyping, though is not quite production ready. Nevertheless, developers are big fans of this new feature. We’ll keep you posted as any new updates come about. Stay tuned!

What else is happening in the PHP landscape? Learn more about the state of PHP in our latest report. 

DOWNLOAD REPORT