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?
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".
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With