Tutorial :Is it possible to use C++ binaries from php



Question:

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?


Solution:1

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

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


Solution:2

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.


Solution:3

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


Solution:4

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


Solution:5

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


Solution:6

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


Solution:7

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.


Solution:8

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?


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »