Why PHP 7.4 and FFI excites me

Submitted by m_sabal on Fri, 07/26/2019 - 12:04

In May 2019, the creator of the PHP language, Rasmus Lerdorf, spoke at the PHP day conference in Verona, Italy. His presentation is available on YouTube (https://www.youtube.com/watch?v=wCZ5TJCBWMg), and I would encourage all PHP developers to watch it. Towards the end of his presentation, Rasmus highlighted some of the features that will be added to the language in version 7.4, scheduled for release in late November 2019. In my opinion, the most exciting of these features is the Foreign Function Interface, abbreviated FFI.

Up to now, if you needed access to a library of functions outside PHP, such as those contained in SO or DLL files, you had to create a PHP extension. These extensions, in many cases, haven't been portable from one system to another, because there is no guarantee that the system provider has installed or enabled them. The result is that any application written in PHP which requires these extensions also requires the application administrator to have full control of the machine PHP is running on. FFI brings library binding into the application space, which now allows developers to simply include the SO or DLL they need in the package, or provide an installation mechanism to download the package from its repository into the application directory.

This feature has existed in many other languages for years, and there have been many instances where I've wished for it in PHP. In particular, many machine learning (AI/ML) libraries are written in C or C++ and provided as SO and DLL libraries. The state of the art in machine learning is moving at so rapid a pace, machine learning paradigms are shifting roughly every six months. While PHP is not known for its statistical analysis, there is no reason why it couldn't be. In fact, for many machine learning scenarios, I think PHP could be a better choice than existing alternatives, if access to neural network libraries were available in the application space. Here's why:
1) In general, PHP executes many times faster than Python, Java, or R, the current favorites for machine learning.
2) For real-time training scenarios, PHP can parse and process chunked data much more quickly than other languages, meaning less real-time data is lost.
3) Because of the ubiquity of PHP on hosted platforms, new instances can be launched at a fraction of the cost of launching a full virtual machine or container. Applications written with REST, SOAP, JSON, or similar APIs could be spread over hundreds or thousands of sites, making parallel processing simpler and more cost effective than maintaining system clusters.

I'm not suggesting that PHP is going to be the best tool for every kind of ML task. Models trained on data that doesn't change much, particularly those that are specific to a particular domain, are probably better handled on platforms that can be dedicated to the task. But as ACM award winner Yann LeCun shared during his recent July 2019 presentation, The Power and Limits of Deep Learning (an updated version of the November 2017 presentation available on YouTube), algorithms need to learn how to build accurate models with less data, and update those models when new data suggests the environment has changed. Our world is in a constant state of change, and PHP applications are well-positioned to observe and adapt to those changes as they happen. Having machine learning libraries available to those applications can make for a better technology experience for companies and consumers alike.

Here is the negative with what I'm suggesting. If you want to play with PHP 7.4 and FFI now, you will have to compile the language binaries from source (https://downloads.php.net/~derick/). Applications written to use these new features will not be portable to most hosting platforms for a number of years. Many companies only just started offering 7.0 earlier this year, and still do not have 7.3 as an option. Many operating system distributions likewise limit their PHP options to older versions. However, if you are able to install version 7.4, and would like to learn how to take advantage of FFI, you can find the documentation here, https://www.php.net/manual/en/book.ffi.php.