0Pricing
PHP Academy · Lesson

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 installed

All lessons in this course

  1. How the Zend Engine Works
  2. Memory Management and Garbage Collection
  3. OPcache and JIT Compilation
  4. Writing a Basic PHP Extension in C
← Back to PHP Academy