The PHP ecosystem today offers some of the best solutions for this.
1. Write a PHP extension
You can use Zephir to easily create PHP extensions in a PHP-like language. They provide a mechanism called custom optimizers that clearly associates custom C libraries or custom C code with your extension.
Although this requires a bit more work, it will lead to performance close to C using native PHP calls and will update your PHP fu. You can also accidentally fall in love with Phalcon.
This Github comment provides a brief overview of why you should use the Zephir optimizer on top of the built-in CBLOCK and how to structure your directories / files.
2. Use FFI
Starting with PHP 7.4, the external function interface is supplied built-in. You can also install it as an extension for older versions of PHP.
Top? You do not need to write your own PHP extension in order to use C code. Back side? Currently, FFI's performance cannot match the real C code. You can think of it more like a crutch than a Batmobile.
If you care about high performance, try option 1.
source share