My experience in porting an extension to PHP5

My experience in porting an extension to PHP5.

One of the mysteries of PHP is how people actually figure out how to
write C extensions. Most books get quickly out-of-date, and key API's
are only documented in the brains of the elite hackers. The C source is
incomprehensible, thanks to the extensive use of weird macros.
Fundamentally, I suspect budding PHP extension hackers are masochists
who enjoy the pain of reading other people's obfusticated C.

Now that I have revealed the secret to writing PHP extensions
(a highly developed inability to distinguish between pain and
pleasure), let's get on with the story. I wrote the ADOdb extension
a couple of years ago to speed up key ADOdb functions. You can get a
speedup of up to 100% when the extension is installed. ADOdb
autodetects and uses the extension if it is installed.

A few weeks ago, I was getting reports from people who were
unable to compile with PHP5. I guess I'm no longer a true hacker, as I
couldn't bear the pain of investigating this issue until this weekend.
A quick test revealed that the actual problem was simple; the PHP
parameter passing API had changed, and the BYREF_FORCE constant is no
longer needed (don't ask why – i have no idea – i just copied the code
from the mysql extension). This was a 3 line change to get the
extension compiled and working.

Since I was fixing PHP5 compatibility, I decided to implement
some new speedups after asking some questions on the php-internals
newsgroup – thanks Andrey, George and Wez! The ADOdb extension with the
new speedups appear to be stable on Linux.

PHP5 on Windows is another story. Even without the ADOdb extension,
Apache 1.3 with PHP5 installed is crashing occasionally. This means I
have no way of telling whether a crash is because of my bad code, or
PHP5 Windows bugs. I know I should file a bug report, but I don't
really have time to dig further.

In the time honoured way of open source and the PHP5 release, I have
released this extension to the world. Let's hope that my C programming
skills justify the short weekend of testing I gave to the extension.

Last night, after releasing the extension, I realised that the
new ADOdb extension is not thread-safe (which is not an issue on Apache
1.3 on *NIX– this configuration doesn't use threads). To figure things
out in the PHP source is not easy. Good guesswork, and lots of luck
helps. This morning, I did a search for “thread safe Zend Engine” in
Google. No luck. My backup plan was to search the PHP source. A search
for “thread” seemed too simple. Hackers like obscure computer science
terms: a search for “mutex” worked – I found the right functions.

I've already implemented the thread safe changes.
However I'm not confident enough to release it. I have no idea whether
the coding is correct (though it runs), because i cannot reliably test
it while PHP5 remains unstable on Windows. 
[PHP Everywhere – Thoughts on PHP and other programming technologies by John Lim]

Leave a comment