简体   繁体   中英

Generic bridge between C++ and PHP

We have a web application (quite a heavy-weight one) that does a whole bundle of stuff, web ui, mathematical processing and modelling, and database-related stuff.

A number of the processes that happen in PHP are quite slow, mostly because PHP sucks for mathematical processing.

In the future, we'd like to break these slow process off, and rewrite them in something faster, probably C++, or maybe Java.

Does anyone know of a generic way to get data in and out of PHP and into another language's environment? I've already considered serialising things into JSON and shovelling them into /dev/shm or a socket, then have the other language listen on a socket, or look at the shared memory location etc.

This kinda thing works fine for native types, but does suck quite hard when you consider that a lot of the stuff that gets passed around are actually class instances and similar objects.

I'm not sure of a sensible solution, but I've been considering Quercus PHP implementation which is pure Java, as well as the PHP/Java bridge.
However, I've been unable to find anything for C++ that would do a similar function. Writing a PHP module is a possibility, but I'm struggling to see how it could be sufficiently generic, and not just a case of rewriting each new (and some old) script into a module, then calling those functions in PHP.

Google protocol buffers

Protocol Buffers

What is it?

Protocol Buffers are a way of encoding structured data in an efficient yet extensible format. Google uses Protocol Buffers for almost all of its internal RPC protocols and file formats.

<endquote>

It supports fast and efficient serialization for objects that you define in the .proto language, and supports C++/Java natively, and PHP with a 3rd party add on.

Have a look at Gearman: http://gearman.org/

It works with C and PHP, amongst a heap of other languages. It doesn't explicitly state C++ though :(

Apart from working as a bridge, it can work as a load balancer and give system redundancy.

I haven't used it myself, but it seems pretty awesome :)

EDIT: Gearman would require you to serialize the data though, so a minus in that regard.

We are currently using zend framework to call C++ libraries in our php code. It might be what you are looking for.

Cheers!

I have just completed development of the "PHP file object", which is a layer 10 component of the Vettrasoft Z Directory. This object allows calling 'pre-processing' of PHP code, embedded in a document, from a C++ program. The application code would look something like this:

int i;
file_o f_in("mytemplate.txt"), f_out("my_output.txt");
rec_dbag_o bag;
phpfile_o my_php (f_in);

for (i=0; i < 9; i++)
{
    my_php.process(f_out, bag);
    // --do something with 'f_out' (PHP generated output file)--
}

The idea here is that the input file ('f_in' in the example above) is a "template" file that contains PHP code (or just a straight PHP file). The generated output goes into 'f_out' [above]. Typically this would be in a loop. The data from the databag ('bag', above) is provided to the PHP processor (databags are Vettrasoft's alternative to XML) and hence transfers data from your C++ app to the PHP code.

Unlike some of the alternatives mentioned, you just link your program to the Z Directory libraries. However, it does require a native PHP 'CLI' [command-line interface] processor (a "SAPI" program) on the same machine (if you can do "http://localhost/somfile.php", you probably have PHP configured).

This is actually the latest addition to the Z Directory that, as of this writing (Jun 27 2012), has not been released yet and we dubbed it the "C++/PHP bridge". It passed alpha just today and will be included in the next release (ZP9/b13) - about mid-July. A friend pointed out this post in stackoverflow.com, it was eye-popping for me to read this since the timing is so coincidental.

I've recently stumbled upon the Hiphop optimizer. Facebook uses this, it might be just what you need,though i can't speak from experience. https://github.com/facebook/hiphop-php/wiki/

See if it suits your needs :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM