w3schools - php wiki




Is it possible to use C++ binaries from php (6)

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++ ?

If so, please tell how can I do so?


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.

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 :)


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.

Take the Mcrypt function for example, this will just be a PHP wrapper to the libMyCrypt library.

Interesting that you say, "becuase it's really needed in enterprise applications". What's missing from PHP current library set that you need?



You can write extensions in pretty much any language and use them with PHP via a dynamically linked library.


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).


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 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.

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.







php