如何为 PHP 应用程序开发 C 扩展?
我的 PHP 应用程序有一个数字运算部分,对于 PHP 来说速度很慢,所以我正在考虑构建一个自定义 C 扩展,但不可能找到任何好的参考来开始:(
有关于如何做的指南吗?像这样的东西?
My PHP app has a number-crunching part that is just to slow for PHP, so I was thinking of building a custom C extension, but it is impossible to find any good reference to start with :(
Is there a guide on how to do something like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最好的资源是 扩展和嵌入 PHP,作者:Sara Golemon。更过时的是 PHP 文档。另一方面,PHP wiki 上的内容非常最新,但也相当不完整并且不太适合初学者。另请参阅这些文章,高级 PHP 编程,作者:George Schlossnagle,PHP 编程,作者:Rasmus Lerdorf 和 Kevin Tatroe,特别是 这些幻灯片。
最后,您会找到的最权威的来源是 源代码 与 PHP 捆绑的扩展。
The best resource, although outdated in several aspects (it only covers PHP until version 5.1) is Extending and Embedding PHP by Sara Golemon. Even more outdated is the PHP documentation. On the other hand, the content on the PHP wiki is very up-to-date, but also quite incomplete and not very oriented for beginners. See also these articles, part V of Advanced PHP Programming by George Schlossnagle, chapter 14 of Programming PHP by Rasmus Lerdorf and Kevin Tatroe and, specially, these slides.
Finally, the most authoritative source you'll find is the source code of the extensions bundled with PHP.
这可能不是一个答案,而更像是一个建议,有一些工具可以将你的 php 编译为可执行文件,然后你可以将其用作扩展。这会稍微统一您的代码并统一您的项目。我不久前尝试过类似的事情。编译后的 php 的行为与编译后的 c 没有什么不同。
This might not be an answer but more like a suggestion, There are tools out there to compile your php to an executable, which you could just then use as an extension. This Would uniform your code a bit and unify your project. I have tried something like this a while ago. The compiled php acts no differently than the compiled c would.
另一种选择是用 C 编写的命令行工具,您可以从 PHP 启动并通过 stdin/stdout 进行通信。在许多情况下,这更容易编写和部署,但最终取决于您的用例。
Another option would be a command line tool written in C, that you start from PHP and communicate with over stdin/stdout. In many cases this is much easier to write and to deploy, but it ultimately depends on your use case.