Be determined to solve challenging problems in Programming

Marcos Marcolin • November 28, 2022 • 6 min read

php c extensoes phpsrc

This article is meant to share an experience I recently had, to show that persistence and communication are sometimes even more important than technical knowledge itself.

Context

I was stuck on a task that wasn't really my responsibility, it maybe didn't even have a clear owner, but I knew it was needed to scale an application. Being naturally curious, I got involved to grow my knowledge and learn something new, since I enjoy challenging problems.

This 'problem' had already been around for a few years at the company I work for, and some people had even tried to solve it and failed, I believe they didn't even try hard enough, since they're people with more experience and logical ability. However, even though I have plenty of flaws in programming, and even more in social life, I have one strong quality: persistence. If I notice a coworker struggling with something, or even better: hand me a problem I can sink my teeth into, and I'll see it through to the end.

Problem

The challenge itself was maintaining a legacy extension written in the C language (security-related), that connects to PHP's core to perform a certain action. The core issue was updating it to be compatible with 'newer' PHP versions, like 7.4 and 8.0. Today it's only functional up to version 7.3 and had incompatibilities with OPcache.

Before going on, I need to tell you that I don't know the language's Core, I'm not a C programmer, but I have a solid programming foundation, I can read code regardless of the language, figure out how it works even if not 100%, and I also know, in part, how PHP works, or rather, what a script's lifecycle looks like, among other small things.

Starting from there, I began researching and studying how creating, running, and compiling an extension works. Within a few days I already understood parts of how it worked, had already written a few examples and compiled some extensions, and all of that consumed several good nights of studying.

About 2 weeks later, I had already arrived at an alternative solution, presented it to the company's leadership, and solved the problem. However, and fortunately, it later didn't pass security testing, but I got full support (I'll tell that story in another article).

Solution

In the previous section, I mentioned that my alternative solution wasn't approved, but being persistent, I didn't give up and went back to my study routine.

The nights I spent at home were dedicated to solving the problem, but it wasn't easy, it wasn't my area. Every step forward was an indescribable joy, since I dreamed about a solution for several nights, tried to fit it in, and it wouldn't work at all, until I'd manage to make progress.

That reminded me of a phrase I heard from a current Director of the Company (who was the Development manager back then), during a feedback session he told me:

You don't need to know everything, sometimes you just need to know who does

Those weren't the exact words, but close to it, and I never forgot them. That person is Luciano Vaz.

So I thought: why not turn to the PHP community?! I know how important it is and how active it is, but I don't know anyone in Brazil who works with this: PHP extensions in C. I believe there are some, but I don't have the network for that yet.

So I went after some people involved with the project (extension) and sent a simple, but direct, email. Some didn't reply, but one guy did, Johannes Schlüter.

Johannes' reply

I'm not fluent in English, so I translated the message with Google and sent it. He replied the next day saying he wasn't involved with PHP anymore and suggested I talk to Derick Rethans. Who's he? A major contributor to the language's core, and best known for Xdebug, whom I admire a lot.

So there I go again, figuring he wouldn't even reply... I tweaked a few lines of the message and sent it to him. Bam! He replied the next day (or maybe the same day, because of the time zone), see below:

Derick's reply

He replied addressing every point in the email, suggesting I check out a similar solution on GitHub, mentioned he had compiled/tested the extension on versions 5, 7, and 7.3 (running into issues), identified a memory leak that could be fixed, and also how long it would take to fix it.

I was incredibly excited by the response, so I started 'putting things together': the solution he pointed me to, what we already had done by other people, and what I'd done during this time.

// ZEND_ENGINE version definition
#if ZEND_MODULE_API_NO >= 20151012 && ZEND_MODULE_API_NO < 20200930
#define PHP_ZEND_ENGINE_7_0
#endif

#if ZEND_MODULE_API_NO >= 20200930
#define PHP_ZEND_ENGINE_8_0
#endif
// Validation against the previously defined ZEND_ENGINE version
#ifdef PHP_ZEND_ENGINE_7_0
    value = zend_compile_string(&ctt, name TSRMLS_CC);
#endif

#ifdef PHP_ZEND_ENGINE_8_0
    value = zend_compile_string(Z_STR(ctt), name);
#endif

There it was, a few nights and C adjustments later, I had the solution working perfectly on PHP 7.4 and 8.0, it came down to version compatibility issues (and not only that), since every language release brings changes to the engine. From there on it was pure joy, the satisfaction of pulling it off left me proud, confident, since it took several nights of getting involved, thinking, racking my brain, often banging my head against a wall for not having the necessary knowledge, feeling inadequate every now and then, but I was always there, persisting.

Conclusions

When I presented the solution to my employers, they could hardly believe it, I noticed looks of surprise, not doubting my ability (I feel like they trust me), but at actually getting the solution we needed.

The lesson I'd like to pass on is that to solve a challenging problem, be persistent, take initiative, no matter the size or the outcome, go after it, talk to coworkers, ask questions, but don't jump ship, even if you don't have the necessary knowledge.

During the time I spent researching and pushing forward, I learned things I never imagined I would, and they've certainly added to my career.

To you, reader, this might seem silly, or might even seem easy to solve. But for me it was one of the best feelings I've had in this field so far, the feeling of a job well done, knowing I delivered real value to our team.

Long live the community, long live the PHP language, long live open source!

Thanks for your attention, and see you in the next story!

Share: