Innovate faster and cut risk with PHP experts from Zend Services.
See How Zend Helps Leading Hosting Providers Keep Their Managed Sites on Secure PHP
Learn PHP from PHP experts with free, on-demand, and instructor led courses.
Submit support requests and browse self-service resources.
Many developers use a foreign function interface in PHP. Here we cover what it is and how to use it.
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.
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:
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:
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:
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.
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.
It’s fairly simple to start using this interface in PHP:
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.
Instantiate FFI with that header file and/or the library that you’re trying to load.
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.
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.
get the PHP REPORT
Director of Product Management, Zend by Perforce