Edward Touw

Building native PHP extensions with C++

Written by Edward Touw on

Combining PHP and C++ is an unnecessarily difficult process, says Emiel Bruintjes, CTO of Copernica Marketing Software. That’s why he initiated PHP-CPP, an open source project focused on creating a C++ library for developing PHP extensions a much easier way.

Copernica Marketing Software was mainly created using PHP. One of the issues with this language, as Bruijntjes experienced, is that it’s way slower than C++ and uses a lot more CPU.

Wanting to implement some C++ to unclog processes and relieve pressure on CPU usage, he started to study the internals of the PHP engine. But despite the fact that this is the only way for developers to combine PHP and C++, Bruijntjes found out that it was far from easy to use.

Time consuming

‘PHP extensions are very hard to implement and require a deep knowledge of the Zend engine and pointer manipulation,’ he says. ‘Acquiring this knowledge however, is far from easy to do, as clear documentation is barely available.’

‘Even if I took the time needed to understand how to develop PHP extensions using the normal PHP API, other Copernica developers would have to do so as well. It was just too time consuming, and not worth it.’

Enter PHP-CPP

Bruijntjes decided to take another route and initiated , an open source project that is focused on creating a C++ library for developing PHP extensions in a much easier way.

‘Extensions developed with PHP-CPP are a lot easier to understand and simpler to maintain because the code looks much more straightforward than the code of extensions written in C,’ says Bruijntjes.

Lego

According to him, the difference between using the normal PHP API and PHP-CPP is comparable to a tangled up ball of yarn versus Lego. ‘With PHP-CPP it’s just like clicking two blocks together. It’s that easy. It will make life a lot easier for developers of PHP extensions.’

‘Right now, most people just decide to just plugin some extra servers to compensate for the extra CPU usage PHP generates. It doesn’t have to be that way. Or at least, with PHP-CPP it doesn’t.’