Current Location: Home> Latest Articles> How to Create PHP7/8 Extensions from Scratch Using C++ | PHP Development Guide

How to Create PHP7/8 Extensions from Scratch Using C++ | PHP Development Guide

M66 2025-07-07

Introduction

With the continuous development of PHP, more and more developers are focusing on writing and using PHP extensions. C++, as a powerful programming language, provides more functionality and performance optimizations, which is why many developers choose C++ to write PHP extensions. This article will guide you step by step on how to create a PHP extension based on C++ from scratch, providing detailed code examples.

Understanding PHP Extensions

Before we begin, it's important to understand what a PHP extension is and its purpose. A PHP extension is a code library written to add new features and functionality to PHP’s core. By writing an extension, you can directly extend PHP’s capabilities without modifying its source code. PHP extensions can be written in either C or C++, with C++ providing greater flexibility and power.

Setting Up the Development Environment

Before you start writing a C++ PHP extension, you need to set up your development environment. The steps are as follows:

  • Install PHP: You need to install PHP on your development environment and ensure you have the PHP development package installed. You can download the PHP installation package from the official PHP website.
  • Install a C++ Compiler: Before writing C++ code, you need to install a C++ compiler. Common C++ compilers include GCC (GNU Compiler Collection) and Clang.
  • Configure the Development Environment: After installing PHP and the C++ compiler, you need to configure the correct build environment. Detailed configuration steps can be found in their respective official documentation.

Creating the PHP Extension

Next, we will create a basic C++ PHP extension and add some simple functionality. Below is a sample extension code, and we will explain each part in detail:

#include "php.h"

#define PHP_MY_EXTENSION_EXTNAME "my_extension"
#define PHP_MY_EXTENSION_VERSION "1.0"

zend_module_entry my_extension_module_entry = {
    STANDARD_MODULE_HEADER,
    PHP_MY_EXTENSION_EXTNAME,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    PHP_MY_EXTENSION_VERSION,
    STANDARD_MODULE_PROPERTIES
};

PHP_MINIT_FUNCTION(my_extension) {
    return SUCCESS;
}

PHP_MSHUTDOWN_FUNCTION(my_extension) {
    return SUCCESS;
}

PHP_RINIT_FUNCTION(my_extension) {
    return SUCCESS;
}

PHP_RSHUTDOWN_FUNCTION(my_extension) {
    return SUCCESS;
}

PHP_FUNCTION(my_extension_hello) {
    php_printf("Hello World!");
}

zend_function_entry my_extension_functions[] = {
    PHP_FE(my_extension_hello, NULL),
    {NULL, NULL, NULL}
};

PHP_MINFO_FUNCTION(my_extension) {
    php_info_print_table_start();
    php_info_print_table_row(2, "my_extension support", "enabled");
    php_info_print_table_end();
}

zend_module_entry *get_module() {
    return &my_extension_module_entry;
}

extern "C" {
    ZEND_GET_MODULE(my_extension)
}

This code includes the basic structure of the extension, module initialization, request handling functions, etc. Each part is explained in detail via comments.

Compiling and Installing

After completing the code, you need to compile and install the extension. The steps are as follows:

  • Create the config.m4 file: Create a file named "config.m4" and add the necessary configuration to ensure support for the extension.
  • Execute the Compilation Commands: In your terminal, navigate to the extension directory and execute the following commands:
  • phpize
    ./configure --enable-my_extension
    make
    make install
  • Enable the Extension: After compilation, open the php.ini file and add the following line:
  • <span class="fun">extension=my_extension.so</span>
  • Restart PHP: After restarting the PHP server, the extension will be enabled.

Usage Example

Here’s a simple PHP code example demonstrating how to use the extension we created:

<?php
my_extension_hello();
?>

When you run the code, you should see the output "Hello World!".

Conclusion

Through the introduction and code examples provided in this article, you should now have a good understanding of how to create a PHP extension using C++ and how to compile and install it. It’s important to note that this article is just a simple example, and there’s much more to PHP extension development, including topics like memory management, exception handling, and object-oriented programming. We hope this article helps you get started with PHP extension development and inspires you to explore more complex extensions using C++.