I have a PHP web application that needs to call a function in a C++ library. This library is provided by a vendor (libfoo.a on a linux machine).
-
My first instinct is to create a C++ executable that links against libfoo.a, and passes command-line parameters to the function. The PHP web application can then do a system() call to my c++ executable. This would be easy to implement. My concern is whether it would add a lot of overhead to create a new system process for each call. How much would this overhead be?
-
An alternative is that I could use SWIG to wrap the C++ function in a PHP extension, but I don’t have the C++ source code. Does SWIG support linking with a “.a” library? Would it require every other engineer on my team to change their PHP configuration to build in libfoo.a?
If the overhead of the system() call is small (< 30 ms), I would prefer option #1, as it seems much simpler to create the C++ executable once, and not build it into the PHP application. What are your recommendations on the two options?
Not sure what you did finally but I’ve done a simple php wrapper extension years ago calling a C++ lib. Like this you will not have overhead of a system call. I’ts not an issue for you, but you’ll have finer control over calls to the lib. For example keeping the wrapper loaded in memory as opposed to loading it on each call, keeping custom configuration parameters etc. I depends on the nature of your lib.
Just some references that might be of interest: