Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Getting Started with PHP Extensions

Getting Started with PHP Extensions

Lochemem Bruno Michael

October 22, 2020
Tweet

More Decks by Lochemem Bruno Michael

Other Decks in Programming

Transcript

  1. Lochemem Bruno Michael • PHP enthusiast from Kampala, Uganda •

    Functional Programming aficionado • Graduated college in 2019 • Maintains PHP userland packages and extensions • Loves ReactPHP • Authored a book • Loves hoops, music, movies, podcasts, and video games
  2. So, PHP extensions? • Written mainly in C/C++ • Infuse

    the PHP userland with ad-hoc functionality • Require interaction with a low-level PHP engine API
  3. Why even bother? • Extensions are typically faster than their

    PHP equivalents ◦ Primarily because they are written in C/C++ • Extensions allow for infusion of performant ad-hoc solutions ◦ Addition of features otherwise not present in PHP userland (ext-uv, ext-apcu) ◦ Either unique projects or wrappers around other C/C++ packages • Extension-building likely results in a better understanding of PHP internals ◦ Engaging endeavor likely to test your patience and skill
  4. PHP Lifecycle 1. MINIT (Module Initialization) 2. RINIT (Request Initialization)

    3. RSHUTDOWN (Request Shutdown) 4. MSHUTDOWN (Module shutdown) MINIT RINIT MSHUTDOWN RSHUTDOWN
  5. Anatomy of an extension • tests -> *.phpt ◦ for

    module test files • config.m4/config.w32 ◦ for platform-specific configuration options • ext.cpp|.c/[name].cpp|.c ◦ arbitrarily named extension file • php_ext.h/php_[name].h ◦ primary extension header file
  6. What about tries? • Potent tree structures for storing string

    data • Persistent data structures • Designed for fast traversal and string searches • Empty root node with multiple child nodes • Useful as dictionaries and associative maps • Somewhat space inefficient
  7. Salient elements • Extension info (name, version) • PHP-engine header

    file inclusions • Module definition • Other discretionary constants
  8. The scope of the implementation • A single Trie class

    • Public constructor • Two additional methods - insert and search
  9. • Linked list • Stores string keys and values •

    String values are appendable to the end of each final key node C++ trie object
  10. What to expect • Macros, macros, and more macros •

    Engine-specific functions • Memory management • PHP internals conventions
  11. • Basis for the PHP userland object • Contains C++

    trie object PHP-C/C++ trie object
  12. Things to be mindful of when writing functions • PHP

    zvals • Argument parsing • Return values • Memory management
  13. • Zend value container • PHP variables • C union

    • Has unique macros for multiple use-cases (ZVAL_P, Z_TYPE_P, Z_LVAL_P, etc...) zvals
  14. • All manner of types (string, float, array, etc...) •

    Arguments should be relevant to function Argument parsing
  15. • PHP engine API return_value variable • PHP engine API

    return macros • Should translate to PHP userland types Return values
  16. • Allocate something then free it • Use PHP engine

    API helpers • Guards against some nasty leaks and faults Memory management
  17. The .phpt file • De-facto engine test file format •

    Sections vs succinct PHP method assertions (SKIPIF, EXPECT, FILE, etc...) • Trigger tests by using the directive make test
  18. Moving Forward... • Take a look at the Internals book

    • Contact someone knowledgeable • Look at engine code • Sharpen your C/C++ skills