是否可以使用来自 php 的 C++ 二进制文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/705443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to use C++ binaries from php
提问by Pooria
Is it possible to write some C or C++ code and compile to binaries, then use those binaries with php? Is it also possible to write a php library using C and C++ ?
是否可以编写一些 C 或 C++ 代码并编译为二进制文件,然后将这些二进制文件与 php 一起使用?是否也可以使用 C 和 C++ 编写一个 php 库?
If so, please tell how can I do so?
如果是这样,请告诉我该怎么做?
采纳答案by Ferdinand Beyer
PHP is modular in design -- it consists of the "engine" and many extensions, some of which are essential (e.g. the "standard" extension) and some are optional. They can be compiled-in or loaded dynamically (via php.ini settings or the dl()function).
PHP 在设计上是模块化的——它由“引擎”和许多扩展组成,其中一些是必不可少的(例如“标准”扩展),而一些是可选的。它们可以被编译或动态加载(通过 php.ini 设置或dl()函数)。
You can easily write your own PHP extension in C/C++, if you are willing to learn the API. Start with the documentation on how to "hack the Zend Engine".
如果您愿意学习 API,您可以轻松地用 C/C++ 编写自己的 PHP 扩展。从有关如何“破解 Zend 引擎”的文档开始。
回答by RBerteig
You might want to check out SWIG, a general tool for wrapping libraries so that they may be called from a variety of languages. PHP is supported by SWIG, as are Perl and Lua (the targets I've personally used). Quoting from the features list at the SWIG website:
您可能想查看SWIG,这是一种用于包装库的通用工具,以便可以从各种语言调用它们。PHP 受 SWIG 支持,Perl 和 Lua(我个人使用过的目标)也是如此。引用SWIG 网站上的功能列表:
SWIG currently generates wrapper code for eighteen different target languages:
- Allegro CL
- C#
- CFFI
- CLISP
- Chicken
- Guile
- Java
- Lua
- Modula-3
- Mzscheme
- OCAML
- Octave
- Perl
- PHP
- Python
- R
- Ruby
- Tcl
- UFFI
In addition to this, the parse tree can be exported as XML and Lisp s-expressions. Experimental work is also available for a Pike module.
SWIG 目前为 18 种不同的目标语言生成包装器代码:
- 快板CL
- C#
- CFFI
- CLISP
- 鸡
- 诡计
- 爪哇
- 路亚
- Modula-3
- 方案
- 反洗钱
- 八度
- 珀尔
- PHP
- Python
- 电阻
- 红宝石
- TCL
- UFFI
除此之外,解析树可以导出为 XML 和 Lisp s 表达式。实验工作也可用于 Pike 模块。
Some of its features are dependent on back-end support in the per-language wrapper generators, but in general it provides easy to use wrappers for passing all plain-data value types in and out of functions. Where the target language has the concept, it can usually map object models as well.
它的一些功能依赖于每个语言包装器生成器的后端支持,但总的来说,它提供了易于使用的包装器,用于将所有纯数据值类型传入和传出函数。在目标语言有概念的地方,它通常也可以映射对象模型。
回答by Andrei Serdeliuc ?
You could use C++ compiled code inside php by using the system() function and calling your program via the operating system (just as you would on the command line).
您可以通过使用 system() 函数并通过操作系统调用您的程序(就像在命令行上一样)在 php 中使用 C++ 编译代码。
回答by nothrow
It is possible to write php extensions in C++ (but you must also write C facade). It isn't trivial, and I'd say, that if you ask instead of looking into PHP code, you're not so familiar with C and how PHP works inside, and due to it's bad documentation it is a bad idea for you to write library in C.
可以用 C++ 编写 php 扩展(但你也必须编写 C 门面)。这不是微不足道的,我想说的是,如果你问而不是查看 PHP 代码,你对 C 和 PHP 在内部的工作方式不太熟悉,而且由于它的文档很糟糕,这对你来说是个坏主意用C编写库。
EDIT: there is a ... some basic tutorial on zend. http://devzone.zend.com/node/view/id/1021, as I'm looking into it, maybe the documentation changed since my PHP times :)
编辑:有一个......关于zend的一些基本教程。http://devzone.zend.com/node/view/id/1021,正如我正在调查的那样,也许自从我的 PHP 时代以来文档就发生了变化:)
回答by Kylotan
You can write extensions in pretty much any language and use them with PHP via a dynamically linked library.
您几乎可以用任何语言编写扩展,并通过动态链接库将它们与 PHP 一起使用。
回答by Brian Mitchell
You can build shared object extensions that php can then load. There's a short tutorial on this at http://devzone.zend.com/node/view/id/1021
您可以构建 php 可以加载的共享对象扩展。在http://devzone.zend.com/node/view/id/1021上有一个简短的教程
回答by Mark
You can write your code in C/C++ and compile it into an EXE. You can then call exec(); from PHP and execute the code.
您可以使用 C/C++ 编写代码并将其编译为 EXE。然后你可以调用 exec(); 从PHP并执行代码。
回答by Steve Claridge
PHP is calling functions from pre-built libraries for many of the the things that it does. If you have a library of your own written in C or C++ you can create PHP bindings for it and then call it from the rest of your PHP code as you would with any other PHP function.
PHP 正在从预构建的库中调用函数来完成它所做的许多事情。如果您有自己的用 C 或 C++ 编写的库,您可以为它创建 PHP 绑定,然后像使用任何其他 PHP 函数一样从 PHP 代码的其余部分调用它。
Take the Mcrypt function for example, this will just be a PHP wrapper to the libMyCrypt library.
以 Mcrypt 函数为例,这只是 libMyCrypt 库的 PHP 包装器。
Interesting that you say, "becuase it's really needed in enterprise applications". What's missing from PHP current library set that you need?
有趣的是,您说“因为企业应用程序确实需要它”。您需要的 PHP 当前库集中缺少什么?

