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

What's hot (20)

PDF
My 10 favorite Haxe language features - Francis Bourre - Codemotion Rome 2017
Codemotion
 
PDF
Building parsers in JavaScript
Kenneth Geisshirt
 
PDF
Cap'n Proto (C++ Developer Meetup Iasi)
Ovidiu Farauanu
 
PDF
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
Ovidiu Farauanu
 
PDF
Features of go
Manjitsing Valvi
 
PPT
python training | python course | python online training
Nancy Thomas
 
PDF
FregeDay: Roadmap for resolving differences between Haskell and Frege (Ingo W...
Dierk König
 
PDF
Drupal and testing (2010 - 2011 / 2)
Peter Arato
 
PDF
Grant Rogerson SDEC2015
Grant Rogerson
 
PDF
Modularity problems
Юлия Коваленко
 
PDF
Generics, Go
Eddy Kiselman
 
PDF
Introduction to Python
GLC Networks
 
PDF
FregeDay: Design and Implementation of the language (Ingo Wechsung)
Dierk König
 
PDF
A quick introduction to go
Dhanush Gopinath
 
PDF
Groovy as a Dynamic Language
Darren Cruse
 
PDF
Adopting language server for apache camel feedback from a java/Eclipse plugi...
Aurélien Pupier
 
PDF
TypeScript - An Introduction
NexThoughts Technologies
 
PDF
OOPS Advanced
Madhavan Malolan
 
PDF
Introduction to protocol buffer
Tim (文昌)
 
PDF
Indian Language App.Development Framework for Android
Siji Sunny
 
My 10 favorite Haxe language features - Francis Bourre - Codemotion Rome 2017
Codemotion
 
Building parsers in JavaScript
Kenneth Geisshirt
 
Cap'n Proto (C++ Developer Meetup Iasi)
Ovidiu Farauanu
 
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
Ovidiu Farauanu
 
Features of go
Manjitsing Valvi
 
python training | python course | python online training
Nancy Thomas
 
FregeDay: Roadmap for resolving differences between Haskell and Frege (Ingo W...
Dierk König
 
Drupal and testing (2010 - 2011 / 2)
Peter Arato
 
Grant Rogerson SDEC2015
Grant Rogerson
 
Modularity problems
Юлия Коваленко
 
Generics, Go
Eddy Kiselman
 
Introduction to Python
GLC Networks
 
FregeDay: Design and Implementation of the language (Ingo Wechsung)
Dierk König
 
A quick introduction to go
Dhanush Gopinath
 
Groovy as a Dynamic Language
Darren Cruse
 
Adopting language server for apache camel feedback from a java/Eclipse plugi...
Aurélien Pupier
 
TypeScript - An Introduction
NexThoughts Technologies
 
OOPS Advanced
Madhavan Malolan
 
Introduction to protocol buffer
Tim (文昌)
 
Indian Language App.Development Framework for Android
Siji Sunny
 

Similar to Getting Started with PHP Extensions (20)

PDF
Php extensions workshop
julien pauli
 
PDF
Php7 extensions workshop
julien pauli
 
PPTX
Php extensions
Elizabeth Smith
 
PPTX
Php extensions
Elizabeth Smith
 
PPTX
Php extensions
Elizabeth Smith
 
PPTX
Php’s guts
Elizabeth Smith
 
PPTX
Php Extensions for Dummies
Elizabeth Smith
 
PDF
PHP QA Tools
rjsmelo
 
PDF
PHP Internals and Virtual Machine
julien pauli
 
PPTX
Listen afup 2010
Gabriele Santini
 
ODP
Professional PHP: an open-source alternative for enterprise development [Antw...
Combell NV
 
PDF
Create your own PHP extension, step by step - phpDay 2012 Verona
Patrick Allaert
 
ODP
Wc13
Pomaxa None
 
PDF
Professional PHP: an open-source alternative for enterprise development [Kort...
Combell NV
 
PDF
PHP7. Game Changer.
Haim Michael
 
PDF
Php
Vineet Vats
 
PPTX
Php internal architecture
Elizabeth Smith
 
PPT
Introduction to PHP - SDPHP
Eric Johnson
 
PDF
50 shades of PHP
Maksym Hopei
 
PPT
Php basics
sagaroceanic11
 
Php extensions workshop
julien pauli
 
Php7 extensions workshop
julien pauli
 
Php extensions
Elizabeth Smith
 
Php extensions
Elizabeth Smith
 
Php extensions
Elizabeth Smith
 
Php’s guts
Elizabeth Smith
 
Php Extensions for Dummies
Elizabeth Smith
 
PHP QA Tools
rjsmelo
 
PHP Internals and Virtual Machine
julien pauli
 
Listen afup 2010
Gabriele Santini
 
Professional PHP: an open-source alternative for enterprise development [Antw...
Combell NV
 
Create your own PHP extension, step by step - phpDay 2012 Verona
Patrick Allaert
 
Professional PHP: an open-source alternative for enterprise development [Kort...
Combell NV
 
PHP7. Game Changer.
Haim Michael
 
Php internal architecture
Elizabeth Smith
 
Introduction to PHP - SDPHP
Eric Johnson
 
50 shades of PHP
Maksym Hopei
 
Php basics
sagaroceanic11
 
Ad

Recently uploaded (20)

PDF
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
PPTX
Platform for Enterprise Solution - Java EE5
abhishekoza1981
 
PDF
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PPTX
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
PDF
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
PDF
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
 
PPTX
The Role of a PHP Development Company in Modern Web Development
SEO Company for School in Delhi NCR
 
PDF
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
PDF
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PPTX
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
PDF
Executive Business Intelligence Dashboards
vandeslie24
 
PPTX
Tally software_Introduction_Presentation
AditiBansal54083
 
PPTX
Fundamentals_of_Microservices_Architecture.pptx
MuhammadUzair504018
 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
PPTX
Comprehensive Guide: Shoviv Exchange to Office 365 Migration Tool 2025
Shoviv Software
 
PPTX
MiniTool Power Data Recovery Full Crack Latest 2025
muhammadgurbazkhan
 
PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PDF
Understanding the Need for Systemic Change in Open Source Through Intersectio...
Imma Valls Bernaus
 
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
Platform for Enterprise Solution - Java EE5
abhishekoza1981
 
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
 
The Role of a PHP Development Company in Modern Web Development
SEO Company for School in Delhi NCR
 
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
Executive Business Intelligence Dashboards
vandeslie24
 
Tally software_Introduction_Presentation
AditiBansal54083
 
Fundamentals_of_Microservices_Architecture.pptx
MuhammadUzair504018
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
Comprehensive Guide: Shoviv Exchange to Office 365 Migration Tool 2025
Shoviv Software
 
MiniTool Power Data Recovery Full Crack Latest 2025
muhammadgurbazkhan
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
Understanding the Need for Systemic Change in Open Source Through Intersectio...
Imma Valls Bernaus
 
Ad

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