Extending PHP with C++?
Asked Answered
L

3

14

I have a performance intensive routine that is written in PHP that I'd like to port to C++ for a performance increase. Is there any way to write a plugin or extension or something using C++ and interface with it from PHP? WITHOUT manually editing the actual PHP source?

Lockhart answered 10/7, 2009 at 16:48 Comment(1)
Worth noting there's a very similar question here: #492514Toth
N
6

I've written a PHP plugin in C++ with the help of SWIG. It's doable, but it may take a while to get used to the SWIG-compilation cycle. You can start with the SWIG docs for PHP.

Update
As @therefromhere has mentioned, I greatly recommend that you get the book Extending and Embedding PHP. There is almost no documentation to be found online (at least there wasn't in late 2008, early 2009 when I did my PHP plugin). I had to rely on the book for everything. Although sometimes Google Code Search is helpful for finding sample code.

Nesto answered 10/7, 2009 at 21:3 Comment(0)
T
20

As Remus says, you can extend PHP with C/C++ using the Zend API. The linked tutorial by Sara Golemon is a good start, and the book Extending and Embedding PHP by the same author covers the subject in much more detail.

However, it's worth noting that both of these (and pretty much everything else I found online) focus on C and don't really cover some tweaks you need to get C++ extensions working.

In the config.m4 file you need to explicitly link to the C++ standard library:

PHP_REQUIRE_CXX()
PHP_ADD_LIBRARY(stdc++, 1, PHP5CPP_SHARED_LIBADD)

Any C++ library compile checks in the config.m4 file will also require linking the C++ lib:

PHP_CHECK_LIBRARY($LIBNAME,$LIBSYMBOL,,
[
  AC_MSG_ERROR([lib $LIBNAME not found.])
],[
  -lstdc++ -ldl
])

EDIT - and here's how to specify g++:

Last, and not least, in order to choose the C++ rather than C compiler/linker when building the extension, the 6th parameter to PHP_NEW_EXTENSION() should be "yes". ie:

PHP_NEW_EXTENSION(your_extension,
                  your_extension.cpp, 
                  $ext_shared, 
                  ,
                  "-Wall -Werror -Wno-error=write-strings -Wno-sign-compare",
                  "yes")

From the PHP build system manual, the parameters are:

  1. The name of the extension
  2. List of all source files which are part of the extension.
  3. (optional) $ext_shared, a value which was determined by configure when PHP_ARG_WITH() was called for
  4. (optional) "SAPI class", only useful for extensions which require the CGI or CLI SAPIs specifically. It should be left empty in all other cases.
  5. (optional) A list of flags to be added to CFLAGS while building the extension.
  6. (optional) A boolean value which, if "yes", will force the entire extension to be built using $CXX instead of $CC.

I couldn't work out how to get the configure script to set g++ as the compiler/linker instead of gcc, so ended up hacking the Makefile with a sed command to do a search replace in my bash build script:

phpize
./configure --with-myextension
if [ "$?" == 0 ]; then
# Ugly hack to force use of g++ instead of gcc
# (otherwise we'll get linking errors at runtime)
   sed -i 's/gcc/g++/g' Makefile
   make clean
   make
fi

Presumably there's an automake command that would make this hack unnecessary.

Toth answered 27/12, 2009 at 16:33 Comment(3)
Mind tat setting compiler flags this way is a bad practice. This makes it impossible to use different compilers. Either make it compiler dependent or let it to the user. The option can be used for common options like -D though. Additionally it's quite likely that -Wall and -Werror will make trouble - PHP is written in C and C++ in some cases is a bit stricter so you might fail in PHP headers. We fixed many of those issues in 5.4,but some C++ Warnings might still occur.Faletti
@Faletti fair enough, this was project was for internal use only so that wasn't a problem for me.Toth
It might become a problem if you switch to a different platform or compiler one day. clang becomes popular these days for instance. And I mostly added it for future developers hitting this page :-)Faletti
N
6

I've written a PHP plugin in C++ with the help of SWIG. It's doable, but it may take a while to get used to the SWIG-compilation cycle. You can start with the SWIG docs for PHP.

Update
As @therefromhere has mentioned, I greatly recommend that you get the book Extending and Embedding PHP. There is almost no documentation to be found online (at least there wasn't in late 2008, early 2009 when I did my PHP plugin). I had to rely on the book for everything. Although sometimes Google Code Search is helpful for finding sample code.

Nesto answered 10/7, 2009 at 21:3 Comment(0)
E
2

PHP itself a collection of loosely related libraries. See http://devzone.zend.com/article/1021 for a tutorial how to write your own.

Eliza answered 10/7, 2009 at 17:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.