Skip to main content
Created with Avocode.

Secondary Navigation

  • PHP Security Center
  • Blog
  • Store
  • Downloads
    • Downloads
    • Plugins
    • MyZend Account
  • Company
    • About Zend by Perforce
    • Careers at Perforce
    • Customers
    • Partners
    • Press
  • Contact
    • Contact Us
    • Request Pricing
    • Request Support
    • Subscribe
Home
Zend

Main Navigation - Mega Menu

  • Products

    Main Navigation - Mega Menu

    • ZendPHP
      PHP Runtime and Support
    • PHP LTS
      Patches for EOL PHP
    • ZendHQ
      Must-Have Extension for ZendPHP
    • Zend Server
      PHP Application Server
  • Services

    Main Navigation - Mega Menu

    • Service Overview
    • Migration Services
    • Audits
    • CI/CD
    • Custom Consulting
    • Admin as a Service

    Services

    Innovate faster and cut risk with PHP experts from Zend Services.

    Explore Services

  • Solutions

    Main Navigation - Mega Menu

    • PHP Cloud Solutions
    • PHP Container Solutions
    • PHP Security Solutions
    • Windows Solutions
    • Hosting Provider Solutions

    Zend Solutions Cloud

    See How Zend Helps Leading Hosting Providers Keep Their Managed Sites on Secure PHP

    Read More

  • Training

    Main Navigation - Mega Menu

    • Training Overview

    Training

    Learn PHP from PHP experts with free, on-demand, and instructor led courses.

    Explore Training

  • Resources

    Main Navigation - Mega Menu

    • Explore Resources
    • Events & Webinars
    • Papers & Videos
    • Recorded Webinars
    • Blog
    Cloud Orchestration

    Orchestrating Your PHP Applications

    Watch Now
  • Support

    Main Navigation - Mega Menu

    • Explore Support
    • PHP Long-Term Support
    • Knowledgebase
    • Documentation
    • Download Software
    • Download Plugins
    • Request Support

    Support

    Submit support requests and browse self-service resources.

    Explore Support

  • Try Free
  • PHP Security Center
  • Blog
  • Store
  • Downloads

    Main Navigation - Mega Menu

    • Downloads
    • Plugins
    • MyZend Account
    • Downloads
    • Plugins
    • MyZend Account
  • Company

    Main Navigation - Mega Menu

    • About Zend by Perforce
    • Careers at Perforce
    • Customers
    • Partners
    • About Zend by Perforce
    • Careers at Perforce
    • Customers
    • Partners
  • Contact

    Main Navigation - Mega Menu

    • Contact Us
    • Request Support
    • Subscribe

TECHNICAL GUIDE

Writing PHP Extensions

Request PDF Version
Writing PHP Extensions
1. Setting up Your PHP Build Environment on Linux
2. Generating a PHP Extension Skeleton
3. Building and Installing a PHP Extension
4. Rebuilding Extensions for Production
5. Extension Skeleton File Content
6. Running PHP Extension Tests
7. Adding New Functionality
8. Basic PHP Structures
9. PHP Arrays
10. Catching Memory Leaks
11. PHP Memory Management
12. PHP References
13. Copy on Write
14. PHP Classes and Objects
15. Using OOP in our Example Extension
16. Embedding C Data into PHP Objects
17. Overriding Object Handlers
18. Answers to Common Extension Questions

1. Setting up Your PHP Build Environment on Linux

PHP 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-dev

For 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-devel

Now, 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/etc

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.

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.ini

It 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 memory

It makes sense to include your PHP binaries into PATH to override the PHP system: 

$ export PATH=~/php-bin/DEBUG/bin:$PATH

Now we can check that everything works fine: 

$ php -v

You 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 Technologies

Our “DEBUG” PHP build is ready to start development.

Request PDF Version

Book traversal links for 1. Setting up Your PHP Build Environment on Linux

  • ‹ Writing PHP Extensions
  • Writing PHP Extensions
  • 2. Generating a PHP Extension Skeleton ›

Footer menu

  • Products
    • ZendPHP
    • PHP Long-Term Support
    • ZendHQ
    • Zend Server
    • Laminas Enterprise Support
    • PHP Development Tools
  • Services
    • PHP Long-Term Support
    • Migration Services
    • Audits
    • CI/CD Services
    • Custom Consulting
    • Admin as a Service
  • Free Trials and Demos
    • ZendPHP Trial
    • Zend Server Trial
    • ZendHQ Demo
  • Training
  • Resources
    • PHP Security Center
    • Papers & Videos
    • Events & Webinars
    • Recorded Webinars
    • Blog
    • Case Studies
  • Hubs
    • PHP Versions Guide
    • Developing Web Apps With PHP
    • Guide to PHP and IBM i
  • Store
  • Downloads
    • MyZend Account
    • Plugins
    • Container Registry
  • Support
    • PHP Long-term Support
    • Knowledgebase
    • Documentation
  • Contact
    • Request Pricing
    • Request Support
    • Subscribe
  • Company
    • About Zend by Perforce
    • Careers at Perforce
    • Customers
    • Partners
    • Press
Home

Zend by Perforce © Perforce Software, Inc.
Terms of Use  |  Privacy Policy | Sitemap

Social Menu

  • Facebook
  • LinkedIn
  • Twitter
  • YouTube
  • RSS
Send Feedback