Skip to main content

Main navigation

  • Solutions
    • Zend Server
    • ZendPHP Enterprise
    • Laminas Enterprise Support
  • Services
    • Services
      • Services Overview
    • Services Links
      • Services Links Row
        • Migrations
        • PHP Long-Term Support
        • Custom Consulting
        • Audits
        • CI/CD
  • Training
    • Training Featured Links
      • Training
      • Training Bundles
      • Help Me Choose
    • Training Links
      • Training Links Row
        • PHP Training
        • Zend Framework Training
        • Zend Server Training
        • PHP Certification
        • Zend Framework Certification
        • Apache Fundamentals
        • IBM i Training Exercises
  • Resources
    • Resources Featured Links
      • Resources
    • Resource Links
      • Resource Links Row
        • Papers & Videos
        • Events & Webinars
        • Recorded Webinars
        • Blog
        • Case Studies
        • PHP Security Center
  • Support
    • Support Featured Links
      • Support
      • Request Support
    • Support Links
      • Support Links Row
        • PHP Long-Term Support
        • Knowledgebase
        • Documentation
        • Download Software
        • Download Plugins
  • Free Trial

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 Support
    • Subscribe
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 Support
    • Subscribe
Home
Zend

Main Navigation - Mega Menu

  • Products

    Main Navigation - Mega Menu

    • Zend Server
      PHP Application Server
    • Zend Server Full Stack
      Enterprise Support
    • ZendPHP
      PHP Runtime and Support
    • Laminas Enterprise Support
      Formerly Zend Framework
  • Services

    Main Navigation - Mega Menu

    • Service Overview
    • Migration
    • PHP Long-Term Support
    • Audits
    • CI/CD
    • Custom Consulting

    Services

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

    Explore Services

  • Training

    Main Navigation - Mega Menu

    • Training Overview
    • PHP Training
    • PHP Certification
    • IBM i Training Exercises
    • Apache Fundamentals
    • Zend Server Training
    • Laminas Training
    • Zend Framework Certification
    • Training Bundles

    Training

    Beginning to advanced PHP classes to learn and earn global certification.

    Help me choose >

    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
    • Press
    • About Zend by Perforce
    • Careers at Perforce
    • Customers
    • Partners
    • Press
  • 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
    • Zend Server
    • Zend Server Full Stack
    • ZendPHP
    • Laminas Enterprise Support
    • PHP Development Tools
  • Resources
    • PHP Security Center
    • Papers & Videos
    • Events & Webinars
    • Recorded Webinars
    • Blog
    • Case Studies
  • Services
    • PHP Long-Term Support
    • Migrations
    • Audits
    • CI/CD Services
    • Custom Consulting
  • Downloads
    • MyZend Account
    • Plugins
  • Training
    • IBM i Training Exercises
    • PHP Training
    • Laminas Training
    • Zend Server Training
    • PHP Certification
    • Zend Framework Certification
    • Apache Fundamentals
    • Training Bundles
    • Help Me Choose
  • Support
    • PHP Long-term Support
    • Knowledgebase
    • Documentation
  • Store
  • Hubs
    • Developing Web Apps With PHP
    • Guide to PHP and IBM i
    • PHP Migrations Knowledge Base
  • FREE TRIALS
    • Zend Server
    • ZendPHP
  • Company
    • About Zend by Perforce
    • Careers at Perforce
    • Customers
    • Partners
    • Press
  • Contact
    • Request Support
    • Subscribe
Home

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

Social Menu

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