Writing a Basic PHP Extension in C
Build and load your own native extension.
Native Code in PHP
When pure PHP is too slow or you need to bind a C library, you write a PHP extension in C against the Zend API. The extension exposes native functions/classes that PHP calls directly, with no VM overhead.
This lesson builds a minimal hello extension end-to-end: skeleton, function, build, load, and test.
The Build Toolchain
Extensions are built with PHP's phpize, which prepares an autoconf build using the headers of your installed PHP. You need php-dev/php-devel (provides phpize and php-config) plus a C compiler and make.
# Install build prerequisites (Debian/Ubuntu)
sudo apt install php-dev build-essential
# Confirm the tools exist
phpize --version
php-config --extension-dir # where the .so will be installedAll lessons in this course
- How the Zend Engine Works
- Memory Management and Garbage Collection
- OPcache and JIT Compilation
- Writing a Basic PHP Extension in C