Developing PHP Extensions with C++ and PHP-CPP: Advanced

article

#1

Originally published at: http://www.sitepoint.com/developing-php-extensions-c-php-cpp-advanced/

In my earlier articles, I have introduced the PHP-CPP lib to create an extension for PHP using C++ (first article and second article). In the latter, I demonstrated a bit of the OO side of writing a PHP extension with a Complex class doing complex number manipulations.

That introduction is not complete as the main focus of that article is more on the demonstration of the OO capability of PHP-CPP, not on the OO implementation details.

In this article, we will further drill down the Complex lib development, adding more member functions, and addressing some advanced topics in writing a PHP extension with OO features using PHP-CPP:


  • Returning this pointer;

  • Returning a Complex object pointer, i.e., a Complex *;

  • Exposing the __toString magic method;

  • Chaining member function calls;

  • Throwing an exception and handling it in PHP

Continue reading this article on SitePoint


#2

I used to have reservations about PHP-CPP, since it ultimately creates some "boilerplate" code at runtime around the Zend Engine code, meaning that though faster than PHP, you may still not get the best possible performance vs. using the Zend Engine "directly".

I had my eye on Zephir, seeing that it's an entire parser that ultimately converts to actual Zend Engine code that is then compiled, supposedly without runtime boilerplate.

However, this discussion in Zephir's GitHub issues has me worried... It seems Zephir too may add some runtime code, which it is prone to do when you aren't explicit enough all over the place. But then if you are, then you may as well use PHP-CPP to write actual C++ code.

I don't actually use either (yet), mostly because I'm too lazy to go on the "adventure" that is setting up a PHP build environment on Windows. But I'd be very interested to see some more detailed benchmarks of both projects and the binaries coming out of them. Personally, I'd be willing to be as explicit with Zephir as I'd have to be with PHP-CPP, but only if that means having a more efficient code than with PHP-CPP.


#3

Thanks for the sharing on this.

To write an extension with C++ is more straightforward in my view.


#4

Thank you Taylor, really nice serie.
It inspired me to try something myself. I'm not a c++ guy (this is my first attempt...) so i did something orrible i think, but it works quite well.
You can find it here if you wish to take a look:
https://github.com/goddanao/php-czmq - It's an early stage CZMQ 3 (http://czmq.zeromq.org) php binding, wich brings incredible easy message exchange patterns and devices based on ZeroMq;
https://github.com/goddanao/php-zyre - It's an early stage Zyre (https://github.com/zeromq/zyre) php binding, which is an open-source framework for proximity-based peer-to-peer applications.

I really appreciate any feedback.
Andrea.


closed #5

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.