Writing PHP Extensions1. Setting up Your PHP Build Environment on Linux2. Generating a PHP Extension Skeleton3. Building and Installing a PHP Extension4. Rebuilding Extensions for Production5. Extension Skeleton File Content6. Running PHP Extension Tests7. Adding New Functionality8. Basic PHP Structures9. PHP Arrays10. Catching Memory Leaks11. PHP Memory Management12. PHP References13. Copy on Write14. PHP Classes and Objects15. Using OOP in our Example Extension16. Embedding C Data into PHP Objects17. Overriding Object Handlers18. Answers to Common Extension Questions1. Setting up Your PHP Build Environment on LinuxPHP extensions are written in C, and when we develop extensions, we need to care about memory management, array boundaries, and many other low-level problems. Consequently, it’s almost impossible to develop extension from scratch without bugs, and therefore we’ll have to debug them. This is the reason I highly recommend that you create a “DEBUG” PHP build when you setup your PHP build environment on Linux. It will help to detect common errors much earlier.Building PHP from Linux-based sources is not too complicated. However, you first need to install the necessary development components, which include a C compiler, linker, libraries, and include files. Use your Linux package manager to do this.For Ubuntu/Debian:$ sudo apt-get install build-essential autoconf automake bison flex re2c gdb \ libtool make pkgconf valgrind git libxml2-dev libsqlite3-devFor RedHat/Fedora: $ sudo dnf install gcc gcc-c++ binutils glibc-devel autoconf automake bison \ flex re2c gdb libtool make pkgconf valgrind git \ libxml2-devel libsqlite3x-develNow, you can clone the PHP GIT repository from github.com and switch to the sources of the necessary PHP version. (Without the last command, you are going to work with a “master” branch or ongoing new PHP 8.) $ git clone https://github.com/php/php-src.git $ cd php-src $ git checkout php-7.4.1 (switch to tag/branch of necessary PHP version)Next, configure PHP. We are going to build “DEBUG” PHP, install it inside our home directory, and use a custom php.ini file. The “./ configure” command may be extended with additional options, depending on your PHP build requirement. You can specify: Which SAPI (CLI, FastCGI, FPM, Apache) you are going to use. Enable or disable embedded PHP extensions and their options. The full list of possible configuration options is available at “./configure –help.” $ ./buildconf --force $ ./configure --enable-debug \ --prefix=$HOME/php-bin/DEBUG \ --with-config-file-path=$HOME/php-bin/DEBUG/etcUsually, you will need to build PHP in a way that’s similar to your existing binary build. To save time, you can retrieve the configuration options you use for existing builds with the “php -i | grep ‘Configure Command’” and adding it to our “./configure” command. Note that building some PHP extensions may require installation of additional libraries and headers. All the package dependencies are usually checked during this step.Usually, you will need to build PHP in a way that’s similar to your existing binary build. To save time, you can retrieve the configuration options you use for existing builds with the “php -i | grep ‘Configure Command’” and adding it to our “./configure” command. Note that building some PHP extensions may require installation of additional libraries and headers. All the package dependencies are usually checked during this step. Finally, when configure succeeds, we compile and install our PHP build: $ make -j4 $ make install $ cd ..Now we need to create our custom php.ini: $ mkdir ~/php-bin/DEBUG/etc $ vi ~/php-bin/DEBUG/etc/php.iniIt should contain something like the following to enable error reporting and catch possible bugs early: date.timezone=GMT max_execution_time=30 memory_limit=128M error_reporting=E_ALL | E_STRICT ; catch all error and warnings display_errors=1 log_errors=1 zend_extension=opcache.so opcache.enable=1 opcache.enable_cli=1 opcache.protect_memory=1 ; catch invalid updates of shared memoryIt makes sense to include your PHP binaries into PATH to override the PHP system: $ export PATH=~/php-bin/DEBUG/bin:$PATHNow we can check that everything works fine: $ php -vYou should get something like this:PHP 7.4.1 (cli) (built: Jan 15 2020 12:52:43) ( NTS DEBUG ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.1, Copyright (c), by Zend TechnologiesOur “DEBUG” PHP build is ready to start development.Request PDF VersionBook traversal links for 1. Setting up Your PHP Build Environment on Linux‹ Writing PHP ExtensionsWriting PHP Extensions2. Generating a PHP Extension Skeleton ›