External libraries are useful for performance demanding tasks where PHP is simply too slow. Also PHP can work as front-end system for various back-end systems (where server doesn’t provide any PHP supported communication types). I have written some posts about using .Net libraries in PHP so far, but there are some other choices available too. To start with, there are two main categories of possible external code usage in PHP:
- PHP extensions
- COM objects, programs executed with exec()
Today I’m going to look at all of them and explain my choice to use COM objects and not others from above.
Three choices
PHP extensions (how to create them read here) are fast and (at least should be) stable. However they are quite hard to create at start and uses C. For example, I’m now using library written in C# and rewriting it in C just to use it as extension is way to complicated and time consuming (and probably not worth it). I can only image PHP extensions as libraries to optimize some parts of code or to be used in multiple projects. In my case, I only need to some limited tasks.
Next logical choice would be executing programs with exec(). In my opinion it’s the easiest way because programs can be created with any language you like, can be supplied with source code and none of system settings need be changed. If exec() is not blocked in php.ini it’s very practical solution.
Windows users has an option to use COM objects. COM objects stand somewhere between PHP extensions and exec(). They can be created in many languages, but libraries are not executed outside PHP script – they behave as normal PHP objects (almost). They are more convenient to work with than compiled programs, but need to registered with Windows.
The best
I was testing last two solutions for more than two weeks and my final choice is… COM objects. To explain this choice here is a short story:
I have a webpage which allows uploading multiple pictures. Two files are uploaded in parallel and once uploaded they are processed (resized, thumbnail and special structure for DeepZoom is created). All this processing is written in C# and uses various windows and 3rd-party libraries.
At first I tried compiling a program as “console application” (in VisualStudio) and then run it with exec(). It worked as expected, but was a little bit too slow. Script was taking around 3 s. to execute, even though actual program ran in 1 s. I didn’t spent much time analyzing where overhead was coming from but it was clear that it can be optimized.
So I compiled program as “class library”, registered it with the Windows assembly cache and then used with a DOTNET class. First problem was IIS server – it kept throwing 500 errors no matter what I did. I didn’t wanted to waste my time trying to find where was a problem – after changing to COM class it magically started to work (even though it uses the same library). Using my library inside PHP was much more faster and execution time reduced to less than a second (depends on a size of image).
Conclusion
I have tested most of possible ways to execute external code. Even though COM objects perform better they can’t be suggested as default choice, because libraries need to registered with a system what can be problematic and maybe not even possible. So if you have limited permissions to server – use exec(), but in all other cases – I definitely recommend using COM. PHP extensions are even better, but harder to code and limited to C.







