用 C 扩展 PHP?
我有一个用 PHP 编写的性能密集型例程,我想将其移植到 C++ 以提高性能。 有没有办法使用 C++ 编写插件或扩展或其他东西并从 PHP 与之交互? 无需手动编辑实际的 PHP 源代码?
I have a performance intensive routine that is written in PHP that I'd like to port to C++ for a performance increase. Is there any way to write a plugin or extension or something using C++ and interface with it from PHP? WITHOUT manually editing the actual PHP source?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如 Remus 所说,您可以使用 Zend API 用 C/C++ 扩展 PHP 。 Sara Golemon 的链接教程是一个好的开始,并且这本书 扩展和嵌入 PHP 更详细地介绍了该主题。
然而,值得注意的是,这两者(以及我在网上找到的几乎所有其他内容)都集中在 C 上,并没有真正涵盖使 C++ 扩展工作所需的一些调整。
在
config.m4
文件中,您需要显式链接到 C++ 标准库:config.m4
文件中的任何 C++ 库编译检查也需要链接 C++ lib:编辑 - 以下是如何指定 g++:
最后,为了在构建扩展时选择 C++ 而不是 C 编译器/链接器,
PHP_NEW_EXTENSION()< 的 第 6 参数< /code> 应为
“是”
。 即:来自 PHP 构建系统手册,参数为:
$ext_shared
,当为(可选)“SAPI 类”调用 PHP_ARG_WITH() 时由配置确定的值<罢工>
我无法弄清楚如何让配置脚本将 g++ 设置为编译器/链接器而不是 gcc,因此最终使用 sed 命令破解 Makefile,在我的 bash 构建脚本中进行搜索替换:
< Strike>大概有一个 automake 命令可以使这种黑客攻击变得不必要。
As Remus says, you can extend PHP with C/C++ using the Zend API. The linked tutorial by Sara Golemon is a good start, and the book Extending and Embedding PHP by the same author covers the subject in much more detail.
However, it's worth noting that both of these (and pretty much everything else I found online) focus on C and don't really cover some tweaks you need to get C++ extensions working.
In the
config.m4
file you need to explicitly link to the C++ standard library:Any C++ library compile checks in the
config.m4
file will also require linking the C++ lib:EDIT - and here's how to specify g++:
Last, and not least, in order to choose the C++ rather than C compiler/linker when building the extension, the 6th parameter to
PHP_NEW_EXTENSION()
should be"yes"
. ie:From the PHP build system manual, the parameters are:
$ext_shared
, a value which was determined by configure when PHP_ARG_WITH() was called forI couldn't work out how to get the configure script to set g++ as the compiler/linker instead of gcc, so ended up hacking the Makefile with a sed command to do a search replace in my bash build script:
Presumably there's an automake command that would make this hack unnecessary.
我在 SWIG 的帮助下用 C++ 编写了一个 PHP 插件。 这是可行的,但可能需要一段时间才能习惯 SWIG 编译周期。 您可以从PHP 的 SWIG 文档开始。
更新
正如 @therefromhere 所提到的,我强烈建议您阅读扩展和嵌入 PHP 这本书。 网上几乎找不到任何文档(至少在 2008 年末、2009 年初我做 PHP 插件时没有)。 我一切都必须依靠书本。 尽管有时 Google 代码搜索有助于查找示例代码。
I've written a PHP plugin in C++ with the help of SWIG. It's doable, but it may take a while to get used to the SWIG-compilation cycle. You can start with the SWIG docs for PHP.
Update
As @therefromhere has mentioned, I greatly recommend that you get the book Extending and Embedding PHP. There is almost no documentation to be found online (at least there wasn't in late 2008, early 2009 when I did my PHP plugin). I had to rely on the book for everything. Although sometimes Google Code Search is helpful for finding sample code.
PHP 本身是松散相关的库的集合。 请参阅 http://devzone.zend.com/article/1021 了解如何编写的教程你自己。
PHP itself a collection of loosely related libraries. See http://devzone.zend.com/article/1021 for a tutorial how to write your own.