Recent Posts


Open Census Extension and PHP Monkey Patching


Frustrated by Magento? Then you’ll love Commerce Bug, the must have debugging extension for anyone using Magento. Whether you’re just starting out or you’re a seasoned pro, Commerce Bug will save you and your team hours everyday. Grab a copy and start working with Magento instead of against it.

Updated for Magento 2! No Frills Magento Layout is the only Magento front end book you'll ever need. Get your copy today!

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

  1. Clean/clear examples of PHP’s G_INIT and M_INIT, and R_INIT lifecycle functions
  2. Clean/clear examples of using the zend_execute_internal and zend_execute_ex functions to implement a monkey patching system for user-defined and internal functions
  3. 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.

Copyright © Alan Storm 1975 – 2022 All Rights Reserved

Originally Posted: 24th May 2019