tags:

views:

1615

answers:

8

Hi folks,

I was wonder is it possible to write some C or C++ code and compile them and use them inside php ? or also is it possible to write php library using C and C++ ?

If so,please tell how can I do so?

(I think this ganna be useful for all php developers who don't know how to do it,becuase it's really needed in enterprise applications)

+1  A: 

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

Andrei Serdeliuc
Why did this get a down vote? It is feasible and might solve the problem! Down-vote removed.
karim79
is it possible to write php library using C and C++ ? is the system() call REALLY the answer? ;))
Yossarian
I've seen the argument made that separate programs are a better way of sharing code than libraries. It can even make sense under some circumstances, and process creation is lightweight enough in Unix to make it work well enough.
RBerteig
+1  A: 

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

Yossarian
+3  A: 

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

Ferdinand Beyer
A: 

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?

Steve Claridge
Sometimes you have an algorithm that needs to done in prallel for example and performance is a big issue and you want to use that in php, you can write it in C and use it in php
Pooria
+2  A: 

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

Kylotan
+1  A: 

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

Brian Mitchell
A: 

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.

Mark
+3  A: 

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.

RBerteig