Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use C++ binaries from php

Tags:

php

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?

like image 716
Pooria Avatar asked Apr 01 '09 13:04

Pooria


2 Answers

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

like image 76
Ferdinand Beyer Avatar answered Nov 01 '22 22:11

Ferdinand Beyer


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.

like image 12
RBerteig Avatar answered Nov 01 '22 21:11

RBerteig