views:

288

answers:

1

I have a few small libraries and wrappers written in C (not C++) that I would like to make available to PHP via extensions. I read several tutorials on writing proper PHP extensions and it does not seem to difficult, however I don't want the hassle of maintaining the extensions in addition to the libraries.

I read that SWIG supports building extensions that are compatible with Zend PHP 5, which is perfect for me. However, the support seems to be beta according to the SWIG documentation.

My libraries are pretty common, as a meta example of usage in C:

int main(void)
{
    struct libfoo *foo;

    char **tmp;

    foo = foo_init();
    if (foo == NULL) {
        fprintf(stderr, "Could not allocate foo\n");
        return 1;
     }

     tmp = foo_parse(foo, "/foo/foo.txt");
     ......
     foo_finit(foo);
}

Does anyone have any experiences to share with using SWIG to make extensions for libraries as simple as this? Does anyone know of any free/open source projects written in C that are using SWIG to make php extensions as an optional part of the build?

Thanks in advance, I'm hoping to get some feedback. If its positive, I'll just devote some time to really getting to know SWIG.. if not, I'll spend the time just making the extensions by hand.

+1  A: 

A number of years ago I was involved in a project to produce a prototype Python module based on a C API. We decided to use SWIG to get us started since none of the people on the project knew the Python C API. Whilst in principle the idea of having some auto generated functions sounded great in reality it caused us more work since we needed to tweak and customize the SWIG generated code to fit our needs.

From a PHP perspective I maintain a PHP C extension and don't believe that for your "simple" C function(s) going with SWIG will be of help, writing the code by hand will be easier. It may be worth you getting or reading Extending and Embedding PHP by Sara Golemon.

grantc
Thank you, I was only considering SWIG as an effort to save time when updating the extensions as each library updated. I'll also be sure to check out that book, her examples on the PHP dev site are extremely well written, an entire book of them would be very handy :) Thank you for your insights!
Tim Post