What Is a Foreign Function Interface (FFI) in PHP?
Many developers use a foreign function interface in PHP. Here we cover what it is and how to use it.
What Is PHP Foreign Function Interface?
PHP Foreign Function Interface (FFI) is an interface in PHP 7.4 that enables developers to use pure PHP to create extensions and bindings to external (AKA “foreign”) libraries.
They can also use it to call C functions and access C data structures.
Why Is PHP Foreign Function Interface Important?
A foreign function interface in PHP is groundbreaking because previously, developers could only create extensions and bindings to external (AKA “foreign”) libraries — and call C functions and access C data structures using PHP extensions and bindings written in C code. Being able to complete these tasks using pure PHP:
- Saves time.
- Creates new opportunities for PHP developers — such as making it easier to use machine learning in code.
It Can Save Time
Being able to connect to external C shared libraries directly from PHP code using this interface in PHP saves developers time because they can minimize the need to write new PHP extensions and bindings in C.
For example, using the interface, they can:
- Enable rapid prototyping for libraries that do not have PHP extensions.
- Provide solutions for shops that have no C expertise and need to interface with shared libraries, including those from proprietary software. With enough PHP expertise, they can now develop bindings in house.
It Can Create New Opportunities
This interface in PHP has interesting applications with asynchronous PHP including Swoole — and with libraries that have long pre-loading times, such as machine learning models. Plus, because the extension makes it easier to call C functions and C data structures, organizations can develop a piece of code in C to run CPU-intensive workloads faster, and use the interface to connect with it.
Today, leaders in the PHP community are testing the use of this PHP interface to support capabilities that are not often used in PHP applications. For example:
- Dmitry Stogov wrote a proof of concept for FFI using Tensorflow. (It is notoriously challenging to load in PHP.)
- Remi Collet is testing a Redis binding.
What Is the History and Status of FFI?
Foreign function interfaces were initially made available in Python and LuaJIT, and have made those languages very useful for fast prototyping. Today, numerous languages have this type of interface.
Dmitry Stogov, one of the main PHP core contributors and a Zend by Perforce employee, wrote PHP FFI. It was first introduced in PHP 7.4 and is still considered experimental. In 2020, it is likely that the community will add more features to the extension, and improve its performance.
Will It Ever Replace PHP Extensions?
It’s too early to tell, though it is unlikely that this interface in PHP will replace PHP extensions. Many PHP extensions are more than just a binding to a C library using a PHP data object (PDO). However, simpler extensions might be replaced if performance with foreign function interface is on par or better. At the time of this blog’s publication, this interface in PHP is slower than a pure C/C++ extension in virtually every case.
How to Use a Foreign Function Interface in PHP
It’s fairly simple to start using this interface in PHP:
1. Create a Minimal Header File
Create a minimal header file for the library that you want to bind with. The header file (an .h file in C) defines the interfaces that will be available in PHP along with the data types.
2. Instantiate FFI
Instantiate FFI with that header file and/or the library that you’re trying to load.
3. Prepare Data Stuctures
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.
Warning: Because this extension is still experimental and because it enables a log-level interface between systems, you should only use it if you have advanced knowledge of C and the C APIs that are in your code. You can minimize risk by using the ffi.enable directive.
Learn about Trends in PHP Usage
Nearly 80% of all websites are written in PHP. Organizations are using capabilities such as a PHP interface and PHP extensions to help evolve their sites and enable greater innovation using modern web services.
We surveyed more than 500 PHP developers and business leaders about their usage and strategic plans involving PHP. Read the report now.