PHP / C++ Interaction

php.internals

Andrew Mather

20 years ago
Having got a PHP/C extension .so shared library working, (courtesy this mailing list), and now also ported a C++ data server to Linux, which could also be a (C++) .so, the obvious question is: - can I talk to a C++ library from PHP, either directly or via a PHP/C extension? - aka: is there a 'com'/.Net equivalent on Linux to permit language independent calls ? Trivially possible, trivially impossible, or somewhere in-between? Cheers, Andrew.

Sebastian Bergmann

20 years ago
Andrew Mather schrieb:
> is there a 'com'/.Net equivalent on Linux to permit language > independent calls?
http://pecl.php.net/package/ffi might be what you're looking for.
-- Sebastian Bergmann http://www.sebastian-bergmann.de/ GnuPG Key: 0xB85B5D69 / 27A7 2B14 09E4 98CD 6277 0E5B 6867 C514 B85B 5D69

Jeremy Johnstone

20 years ago
On 2/13/06, Andrew Mather <an.dromeda@btconnect.com> wrote:
> > - can I talk to a C++ library from PHP, either directly or via > a PHP/C extension?
You could just have the PHP extension written in C++. There is no requirement of PHP extensions to be written in C, you just need to make sure you properly wrap the C parts inherited from the core w/ an extern C block. I have wrapped more than a handful of C++ libraries at my day job in into PHP extensions using more or less the following approach. Here's a rough overview of the changes needed (this list might not be complete, but should be close). config.m4 changes --------------------------------- I set the the compiler environment variables (aka CC and CXX) to both use g++. Not sure if this is necessary. In your included libraries (aka PHP_ADD_LIBRARY_WITH_PATH) make sure you include libstdc++ your source file changes ----------------------------------- Wrap the following in an extern "C" like shown below: extern "C" { #ifdef HAVE_CONFIG_H #include "config.h" #endif #include "php.h" #include "php_ini.h" #include "ext/standard/info.h" #include "php_your_extension_name.h" } And later on further down wrap the following: extern "C" { #ifdef COMPILE_DL_YOUR_EXTENSION_NAME ZEND_GET_MODULE(your_extension_name) #endif } As best as I can remember, that is all that is necessary to write a C++ PHP Extension instead of a C one. Brighter minds on this list might know better if I am forgetting something, but I remember it being very minimal changes so I think the above is it. -Jeremy
-- --------------------------- Jeremy Johnstone http://www.jeremyjohnstone.com jsjohnst@php.net

Steph

20 years ago
Just search the archives, or the zend weeklies index page (http://www.zend.com/zend/week/) if you're short of time. I remember linking to someone's material regarding C++ extensions for PHP a few months back, and it'll be in the 'item descriptions' there. - Steph ----- Original Message ----- From: "Jeremy Johnstone" <phpdev@gmail.com> To: "Andrew Mather" <an.dromeda@btconnect.com> Cc: <internals@lists.php.net> Sent: Monday, February 20, 2006 3:08 AM Subject: Re: [PHP-DEV] PHP / C++ Interaction On 2/13/06, Andrew Mather <an.dromeda@btconnect.com> wrote:
> > - can I talk to a C++ library from PHP, either directly or via > a PHP/C extension?
You could just have the PHP extension written in C++. There is no requirement of PHP extensions to be written in C, you just need to make sure you properly wrap the C parts inherited from the core w/ an extern C block. I have wrapped more than a handful of C++ libraries at my day job in into PHP extensions using more or less the following approach. Here's a rough overview of the changes needed (this list might not be complete, but should be close). config.m4 changes --------------------------------- I set the the compiler environment variables (aka CC and CXX) to both use g++. Not sure if this is necessary. In your included libraries (aka PHP_ADD_LIBRARY_WITH_PATH) make sure you include libstdc++ your source file changes ----------------------------------- Wrap the following in an extern "C" like shown below: extern "C" { #ifdef HAVE_CONFIG_H #include "config.h" #endif #include "php.h" #include "php_ini.h" #include "ext/standard/info.h" #include "php_your_extension_name.h" } And later on further down wrap the following: extern "C" { #ifdef COMPILE_DL_YOUR_EXTENSION_NAME ZEND_GET_MODULE(your_extension_name) #endif } As best as I can remember, that is all that is necessary to write a C++ PHP Extension instead of a C one. Brighter minds on this list might know better if I am forgetting something, but I remember it being very minimal changes so I think the above is it. -Jeremy
-- --------------------------- Jeremy Johnstone http://www.jeremyjohnstone.com jsjohnst@php.net

Hartmut Holzgraefe

20 years ago
Steph Fox wrote:
> Just search the archives, or the zend weeklies index page > (http://www.zend.com/zend/week/) if you're short of time. I remember > linking to someone's material regarding C++ extensions for PHP a few > months back, and it'll be in the 'item descriptions' there.
Yet another shameless plug: CodeGen_PECL (http://pear.php.net/package/CodeGen_PECL/) supports C++ extension writing just fine (including the needed config.m4 magic etc), see e.g.: http://cvs.php.net/viewcvs.cgi/pear/CodeGen_PECL/docs/examples/resource_cpp.xml?view=markup
-- Hartmut Holzgraefe, Senior Support Engineer . MySQL AB, www.mysql.com