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

18. Answers to Common Extension Questions

To help you learn the basics about PHP, the tutorial we just completed described how to build a very simple practice extension. The following sections provide information that answer common questions and minimize issues relating to: 

  • Links to external libraries. 
  • Naming conventions. 
  • PHP resources 

LINKING EXTERNAL LIBRARIES 

Often, PHP extensions are created to provide binding to some third-party C library. In this case, your PHP extension must be linked with this library. This is done through extension configuration file “config.m4” (or “config.w32” on Windows). For example, the following “config,m4” would be used for zstd extension, that implements binding to libzstd: 

PHP_ARG_WITH([zstd],
    [for zstd support],
    [AS_HELP_STRING([--with-zstd],
        [Include zstd support])])

if test “$PHP_ZSTD” != “no”; then
    PKG_CHECK_MODULES([LIBZSTD], [libzstd])
    PHP_EVAL_INCLINE($LIBZSTD_CFLAGS)
    PHP_EVAL_LIBLINE($LIBZSTD_LIBS, ZSTD_SHARED_LIBADD)

    PHP_SUBST(ZSTD_SHARED_LIBADD)

    AC_DEFINE(HAVE_ZSTD, 1, [ Have zstd support ])

    PHP_NEW_EXTENSION(zstd, zstd.c, $ext_shared)

fi

When an extension uses an external library, we use --with-<feature> option instead of --enabel-<feature>, and therefore PHP_ARG_ WITH() macro instead of PHP_ARG_ENABLE(). Also, a few special macros were added to find the library and include paths, through pkg-config, and add special rules into the final Makefile. 

NAMING CONVENTIONS 

When you start writing a new extension, try to use a consistent naming convention. There are few alternatives: 

  • no prefix (scale). 
  • name prefix with underscore (test_scale). 
  • name prefix and mixed case (TestScale). 
  • namespace (Test\scale) – special variants if the macros (ZEND_NS_NAME, ZEND_NS_FENTRY, ZEND_NS_FE, INIT_NS_CLASS_ ENTRY) should be used to construct namespaced names. 
  • use static class as a namespace (Test::scale) – it’s possible to create a class and declare all functions as static methods of this class. 

PHP RESOURCES 

PHP has one more type: resource. This type was historically used when you had to keep some C data in PHP zval (e.g. file descriptors). It’s represented as a child of zend_refcounted structure with a pointer to some C data structure. 

PHP resources are still used in PHP extensions. However, it’s not recommended to use them for new extensions, because you may implement smarter and more efficient solutions by embedding C data into PHP internal objects. 

Request PDF Version

Book traversal links for 18. Answers to Common Extension Questions

  • ‹ 17. Overriding Object Handlers
  • Writing PHP Extensions

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