How to use external libraries in PHP?

Posted May 23rd, 2009 by Juozas

osoft_1490922690php-logoExternal 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:

  1. PHP extensions
  2. 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.

visualstudio-6Next 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.

expolorer-1Windows 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:

visualstudio-7I 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.

firefox-1At 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.

iis-5So 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.

Trackbacks/Pingbacks

  1. abcphp.com

Comments (2)

  1. Diego Henrique

    The problem is: COM just work on windows. PHP Extensions can work in windows or linux or mac. So, if use windows can’t be a problem, COM is the good choice, but if your server is linux, PHP Extensions is the best choice.

  2. Juozas (author)

    Diego, even though I agree with you that PHP extensions are more portable, but have you actually tried writing one? Not example, not something basic, but proper extension?

    COM extensions are easier to write and also it’s easier to hire a developer to do so. I have seen dozens of titles “we are looking for PHP extensions developers” in open-source projects, though haven’t seen any progress.

    So even though COM extensions are Windows-only and probably slower than PHP extensions (because you just dynamically load COM one), they are some times better, especially for closed-source applications.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">