One of the challenges in learning PHP’s C based internal APIs is finding clear and clean examples to learn from. The C extensions that ship with PHP and pecl are old, often worked on over the years by multiple engineers with no single coding style, and contain all sorts of — let’s call it pragmatic and clever macro BS — that keeps things working across multiple major versions of the PHP engine and the multiple operating systems that PHP supports. Perhaps that’s all old C programs — but irrespective of best practices, the code that ships with PHP isn’t the best when you’re trying to learn core concepts.
That’s why I was pleasantly surprised when I discovered the Open Census PHP extension. Open Census is a project (now part of Open Telemetry) that’s trying to provide open instrumentation software systems via open source code. Their package for PHP includes an optional PHP extension that can automatically instrument PHP functions without needing to liter a codebase with instrumentation calls.
Functionality aside, their PHP extension is worth noting because if offers
- Clean/clear examples of PHP’s G_INIT and M_INIT, and R_INIT lifecycle functions
- Clean/clear examples of using the
zend_execute_exfunctions to implement a monkey patching system for user-defined and internal functions
- Support for PHP 7+ — meaning a lot of the older, legacy internals patterns are nowhere to be found
The disconnect between the wider PHP community and its internals team is unfortunate, but if you’re looking to bridge that gap this is one extension that might help you understand what’s going inside PHP down at the C layer.