Working with PHP Callbacks

Learn how to implement callbacks in PHP 8 applications.

It’s possible to use the FFI extension to incorporate shared C libraries that are part of the actual PHP language (or its extensions). This integration is important as it allows you to read and write native PHP data in your C library by accessing the C data structures defined in the PHP-shared C libraries.

We introduce you to the FFI extension’s ability to override native PHP language functionality. This ability is referred to as a PHP callback. Before we get into the implementation details, we must first examine the potential dangers associated with this ability.

Understanding the dangers inherent to PHP callbacks

It’s important to understand that the C functions defined in the various PHP shared libraries are often used by multiple PHP functions. Accordingly, if we override one of the low-level functions at the C level, we might experience unexpected behavior in our PHP application.

Get hands-on with 1200+ tech skills courses.