简体   繁体   English

C ++和PHP之间的通用桥梁

[英]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. 我们有一个Web应用程序(相当重的一个),可以完成一整套东西,web ui,数学处理和建模,以及与数据库相关的东西。

A number of the processes that happen in PHP are quite slow, mostly because PHP sucks for mathematical processing. PHP中发生的许多过程都很慢,主要是因为PHP很难进行数学处理。

In the future, we'd like to break these slow process off, and rewrite them in something faster, probably C++, or maybe Java. 在未来,我们希望打破这些缓慢的过程,并以更快的速度重写它们,可能是C ++,或者Java。

Does anyone know of a generic way to get data in and out of PHP and into another language's environment? 有没有人知道将数据输入和输出PHP以及进入另一种语言环境的通用方法? 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. 我已经考虑将事物串行化为JSON并将它们铲入/ dev / shm或socket,然后让另一种语言侦听套接字,或查看共享内存位置等。

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. 我不确定一个合理的解决方案,但我一直在考虑Quercus PHP实现,它是纯Java,以及PHP / Java桥。
However, I've been unable to find anything for C++ that would do a similar function. 但是,我一直无法找到可以执行类似功能的C ++。 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. 编写PHP模块是可能的,但我很难看到它是如何足够通用的,而不仅仅是将每个新的(和一些旧的)脚本重写到模块中,然后在PHP中调用这些函数。

Google protocol buffers Google协议缓冲区

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. Google对几乎所有内部​​RPC协议和文件格式都使用Protocol Buffers。

<endquote> <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. 它支持您以.proto语言定义的对象的快速高效序列化,并且本机支持C ++ / Java,并且第三方添加PHP。

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

It works with C and PHP, amongst a heap of other languages. 它适用于C和PHP,是一堆其他语言。 It doesn't explicitly state C++ though :( 它没有显式地陈述C ++ :(

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. 编辑:Gearman会要求你序列化数据,所以在这方面减去。

We are currently using zend framework to call C++ libraries in our php code. 我们目前正在使用zend框架在我们的PHP代码中调用C ++库。 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. 我刚刚完成了“PHP文件对象”的开发,它是Vettrasoft Z目录的第10层组件。 This object allows calling 'pre-processing' of PHP code, embedded in a document, from a C++ program. 该对象允许从C ++程序调用嵌入在文档中的PHP代码的“预处理”。 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). 这里的想法是输入文件(上例中的'f_in')是一个包含PHP代码(或只是一个直接的PHP文件)的“模板”文件。 The generated output goes into 'f_out' [above]. 生成的输出进入'f_out'[上面]。 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. 来自数据库('bag',上面)的数据被提供给PHP处理器(数据库是Vettrasoft的XML替代品),因此将数据从C ++应用程序传输到PHP代码。

Unlike some of the alternatives mentioned, you just link your program to the Z Directory libraries. 与上面提到的一些替代方法不同,您只需将程序链接到Z目录库。 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). 但是,它确实需要在同一台机器上使用本机PHP'CLI'[命令行界面]处理器(“SAPI”程序)(如果你可以做“http://localhost/somfile.php”,你可能有PHP配置)。

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". 这实际上是Z Directory的最新成员,截至本文撰写时(2012年6月27日)尚未发布,我们将其称为“C ++ / PHP桥”。 It passed alpha just today and will be included in the next release (ZP9/b13) - about mid-July. 它在今天通过了alpha,并将在7月中旬的下一个版本(ZP9 / b13)中包含。 A friend pointed out this post in stackoverflow.com, it was eye-popping for me to read this since the timing is so coincidental. 一位朋友在stackoverflow.com上指出了这篇文章,由于时机非常巧合,因此对我来说是令人大笑的。

I've recently stumbled upon the Hiphop optimizer. 我最近偶然发现了Hiphop优化器。 Facebook uses this, it might be just what you need,though i can't speak from experience. Facebook使用它,它可能正是你所需要的,尽管我无法从经验中说出来。 https://github.com/facebook/hiphop-php/wiki/ https://github.com/facebook/hiphop-php/wiki/

See if it suits your needs :) 看看它是否适合你的需求:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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