SlideShare a Scribd company logo
Getting Started with PHP
Extensions
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
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
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
Pump the brakes!
You have to get
familiar with PHP’s
lifecycle
PHP Lifecycle
1. MINIT (Module
Initialization)
2. RINIT (Request
Initialization)
3. RSHUTDOWN (Request
Shutdown)
4. MSHUTDOWN (Module
shutdown)
MINIT
RINIT
MSHUTDOWN
RSHUTDOWN
Traditionally...
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
php_trie
● Trie data structures for PHP
● Built in C++
So, tries huh?
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
The first thing to
do is set things
up.
● Comments
● Configuration option definitions
● Build specifics
Configuring the extension
(for POSIX systems)
● Comments
● Configuration option definitions
● Build specifics
Configuring the extension
(for Windows systems)
Onto the primary
extension header
file...
Salient elements
● Extension info (name, version)
● PHP-engine header file inclusions
● Module definition
● Other discretionary constants
How about the
primary extension
file?
● PHP userland-bound artifacts
● PHP lifecycle handlers
php_trie.cpp’s features
● Extension entry table
● Shared object loading routines
php_trie.cpp’s features
continued...
The next thing to
do is implement
the trie.
The scope of the implementation
● A single Trie class
● Public constructor
● Two additional methods - insert and search
● Linked list
● Stores string keys and values
● String values are appendable to the
end of each final key node
C++ trie object
● Creates a new trie entry
● Appends string to the end of a
branch
Insertion
● Performs trie traversal
● Outputs a matching value at the end
of a branch
search/lookup
Onto the PHP
engine API side
of things...
What to expect
● Macros, macros, and more macros
● Engine-specific functions
● Memory management
● PHP internals conventions
● Basis for the PHP userland object
● Contains C++ trie object
PHP-C/C++ trie object
More PHP object-related stuff
● Object handlers
● Class registration
● The MINIT function
Things to be mindful of when writing functions
● PHP zvals
● Argument parsing
● Return values
● Memory management
● Zend value container
● PHP variables
● C union
● Has unique macros for multiple
use-cases (ZVAL_P, Z_TYPE_P,
Z_LVAL_P, etc...)
zvals
● All manner of types (string,
float, array, etc...)
● Arguments should be relevant to
function
Argument parsing
● PHP engine API return_value variable
● PHP engine API return macros
● Should translate to PHP userland
types
Return values
● Allocate something then free it
● Use PHP engine API helpers
● Guards against some nasty leaks
and faults
Memory management
● Create a trie
● Initializes the Trie object
__construct
● Takes two arguments
● boolean to zend_bool return
value conversion
insert
● Takes one argument
● C++ string to C string conversion
search
How about some
tests?
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
All that’s left is to
build and use the
extension.
● Requires autoconf, make, gcc, and
libtool
● Installs the extension in the PHP
modules directory
Moving
Forward...
● Take a look at the Internals
book
● Contact someone
knowledgeable
● Look at engine code
● Sharpen your C/C++ skills
Thanks.

More Related Content

PDF
Ruxmon.2013-08.-.CodeBro!
ODP
Phpactor and VIM
PDF
A Shallow Survey of Alternative Languages on the JVM
PDF
JDD 2017: Kotlin for Java developers (Tomasz Kleszczyński)
PDF
Error handling
PDF
Learning Python with PyCharm EDU
PDF
Introduction to golang
PDF
Not Your Fathers C - C Application Development In 2016
Ruxmon.2013-08.-.CodeBro!
Phpactor and VIM
A Shallow Survey of Alternative Languages on the JVM
JDD 2017: Kotlin for Java developers (Tomasz Kleszczyński)
Error handling
Learning Python with PyCharm EDU
Introduction to golang
Not Your Fathers C - C Application Development In 2016

What's hot (20)

PDF
My 10 favorite Haxe language features - Francis Bourre - Codemotion Rome 2017
PDF
Building parsers in JavaScript
PDF
Cap'n Proto (C++ Developer Meetup Iasi)
PDF
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
PDF
Features of go
PPT
python training | python course | python online training
PDF
FregeDay: Roadmap for resolving differences between Haskell and Frege (Ingo W...
PDF
Drupal and testing (2010 - 2011 / 2)
PDF
Grant Rogerson SDEC2015
PDF
Modularity problems
PDF
Generics, Go
PDF
Introduction to Python
PDF
FregeDay: Design and Implementation of the language (Ingo Wechsung)
PDF
A quick introduction to go
PDF
Groovy as a Dynamic Language
PDF
Adopting language server for apache camel feedback from a java/Eclipse plugi...
PDF
TypeScript - An Introduction
PDF
OOPS Advanced
PDF
Introduction to protocol buffer
PDF
Indian Language App.Development Framework for Android
My 10 favorite Haxe language features - Francis Bourre - Codemotion Rome 2017
Building parsers in JavaScript
Cap'n Proto (C++ Developer Meetup Iasi)
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
Features of go
python training | python course | python online training
FregeDay: Roadmap for resolving differences between Haskell and Frege (Ingo W...
Drupal and testing (2010 - 2011 / 2)
Grant Rogerson SDEC2015
Modularity problems
Generics, Go
Introduction to Python
FregeDay: Design and Implementation of the language (Ingo Wechsung)
A quick introduction to go
Groovy as a Dynamic Language
Adopting language server for apache camel feedback from a java/Eclipse plugi...
TypeScript - An Introduction
OOPS Advanced
Introduction to protocol buffer
Indian Language App.Development Framework for Android
Ad

Similar to Getting Started with PHP Extensions (20)

PDF
Php extensions workshop
PDF
Php7 extensions workshop
PPTX
Php extensions
PPTX
Php extensions
PPTX
Php extensions
PPTX
Php’s guts
PPTX
Php Extensions for Dummies
PDF
PHP QA Tools
PDF
PHP Internals and Virtual Machine
PPTX
Listen afup 2010
ODP
Professional PHP: an open-source alternative for enterprise development [Antw...
PDF
Create your own PHP extension, step by step - phpDay 2012 Verona
ODP
PDF
Professional PHP: an open-source alternative for enterprise development [Kort...
PDF
PHP7. Game Changer.
PDF
PPTX
Php internal architecture
PPT
Introduction to PHP - SDPHP
PDF
50 shades of PHP
PPT
Php basics
Php extensions workshop
Php7 extensions workshop
Php extensions
Php extensions
Php extensions
Php’s guts
Php Extensions for Dummies
PHP QA Tools
PHP Internals and Virtual Machine
Listen afup 2010
Professional PHP: an open-source alternative for enterprise development [Antw...
Create your own PHP extension, step by step - phpDay 2012 Verona
Professional PHP: an open-source alternative for enterprise development [Kort...
PHP7. Game Changer.
Php internal architecture
Introduction to PHP - SDPHP
50 shades of PHP
Php basics
Ad

Recently uploaded (20)

PPTX
history of c programming in notes for students .pptx
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
Digital Strategies for Manufacturing Companies
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPTX
Transform Your Business with a Software ERP System
PDF
medical staffing services at VALiNTRY
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
top salesforce developer skills in 2025.pdf
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
Introduction to Artificial Intelligence
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
Essential Infomation Tech presentation.pptx
history of c programming in notes for students .pptx
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Which alternative to Crystal Reports is best for small or large businesses.pdf
Upgrade and Innovation Strategies for SAP ERP Customers
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Reimagine Home Health with the Power of Agentic AI​
Digital Strategies for Manufacturing Companies
wealthsignaloriginal-com-DS-text-... (1).pdf
How to Choose the Right IT Partner for Your Business in Malaysia
Transform Your Business with a Software ERP System
medical staffing services at VALiNTRY
Operating system designcfffgfgggggggvggggggggg
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
top salesforce developer skills in 2025.pdf
Odoo Companies in India – Driving Business Transformation.pdf
2025 Textile ERP Trends: SAP, Odoo & Oracle
How to Migrate SBCGlobal Email to Yahoo Easily
Introduction to Artificial Intelligence
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Essential Infomation Tech presentation.pptx

Getting Started with PHP Extensions

  • 1. Getting Started with PHP Extensions
  • 2. 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
  • 3. 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
  • 4. 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
  • 5. Pump the brakes! You have to get familiar with PHP’s lifecycle
  • 6. PHP Lifecycle 1. MINIT (Module Initialization) 2. RINIT (Request Initialization) 3. RSHUTDOWN (Request Shutdown) 4. MSHUTDOWN (Module shutdown) MINIT RINIT MSHUTDOWN RSHUTDOWN
  • 8. 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
  • 9. php_trie ● Trie data structures for PHP ● Built in C++
  • 11. 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
  • 12. The first thing to do is set things up.
  • 13. ● Comments ● Configuration option definitions ● Build specifics Configuring the extension (for POSIX systems)
  • 14. ● Comments ● Configuration option definitions ● Build specifics Configuring the extension (for Windows systems)
  • 15. Onto the primary extension header file...
  • 16. Salient elements ● Extension info (name, version) ● PHP-engine header file inclusions ● Module definition ● Other discretionary constants
  • 17. How about the primary extension file?
  • 18. ● PHP userland-bound artifacts ● PHP lifecycle handlers php_trie.cpp’s features
  • 19. ● Extension entry table ● Shared object loading routines php_trie.cpp’s features continued...
  • 20. The next thing to do is implement the trie.
  • 21. The scope of the implementation ● A single Trie class ● Public constructor ● Two additional methods - insert and search
  • 22. ● Linked list ● Stores string keys and values ● String values are appendable to the end of each final key node C++ trie object
  • 23. ● Creates a new trie entry ● Appends string to the end of a branch Insertion
  • 24. ● Performs trie traversal ● Outputs a matching value at the end of a branch search/lookup
  • 25. Onto the PHP engine API side of things...
  • 26. What to expect ● Macros, macros, and more macros ● Engine-specific functions ● Memory management ● PHP internals conventions
  • 27. ● Basis for the PHP userland object ● Contains C++ trie object PHP-C/C++ trie object
  • 28. More PHP object-related stuff ● Object handlers ● Class registration ● The MINIT function
  • 29. Things to be mindful of when writing functions ● PHP zvals ● Argument parsing ● Return values ● Memory management
  • 30. ● Zend value container ● PHP variables ● C union ● Has unique macros for multiple use-cases (ZVAL_P, Z_TYPE_P, Z_LVAL_P, etc...) zvals
  • 31. ● All manner of types (string, float, array, etc...) ● Arguments should be relevant to function Argument parsing
  • 32. ● PHP engine API return_value variable ● PHP engine API return macros ● Should translate to PHP userland types Return values
  • 33. ● Allocate something then free it ● Use PHP engine API helpers ● Guards against some nasty leaks and faults Memory management
  • 34. ● Create a trie ● Initializes the Trie object __construct
  • 35. ● Takes two arguments ● boolean to zend_bool return value conversion insert
  • 36. ● Takes one argument ● C++ string to C string conversion search
  • 38. 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
  • 39. All that’s left is to build and use the extension.
  • 40. ● Requires autoconf, make, gcc, and libtool ● Installs the extension in the PHP modules directory
  • 41. Moving Forward... ● Take a look at the Internals book ● Contact someone knowledgeable ● Look at engine code ● Sharpen your C/C++ skills