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 Questions18. Answers to Common Extension QuestionsTo 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) fiWhen 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 VersionBook traversal links for 18. Answers to Common Extension Questions‹ 17. Overriding Object HandlersWriting PHP Extensions