Experiences Developing PHP Extensions

In the past I had done a lot of development in C and C++. It used to be the language I worked the most with. C++ can help make developing a complicated application far easier than writing it in pure C. I have moved away into doing more Java work in the last four years. I started with PHP during the latter part of the C/C++ period. I wanted to try to work on developing a PHP extension based on some C and C++ code I had written a number of years ago.

Reading about extension writing appears very intimidating because of the Macros and Zend api aspects. Starting off from scratch this can be very challenging, particularly setting up the structures and the boilerplate as it were for the extension. PHP is written in standard K&R C so that it can be compiled on any platform, which is a great feature, but the code is very intricate, particularly when it comes to object oriented code and reference counting. There have been articles written about wrapping C++ classes into PHP extensions, which seems like a possibility in terms of working with C++ libraries. You physically can build extensions with C++, but for wider distribution on PECL writing your extensions in C is probably the smarter way to go.

I found a Pear project to do the code generation for PHP extensions. This project takes an xml descriptor including code blocks, which generates all the code especially the boilerplate code. It seems to be promissing, It seems it’s best to  continually doing edits in the .xml file and generating new sets of code whenever possible as it makes it easier adding new functions. This approach also adds the necessary support for unit tests. Certain aspects of it seems to not be as well flushed out as could be. In a more complex extension, going beyond what is provided with this project.

PHP uses the the GNU Autotools system, autoconfigure and automake to configure builds. It makes use of M4 macros to manage and extending the basic scripts. It provides a way to interrogate the system and determine the options needed to build a piece of code on a given system. Unfortunately this doesn’t work on Windows. Windows requires a separate build script system in JScript. Aspects of the M4 macros can be challenging to understand which may be a problem with more complex projects.

PHP uses a unit test system using files with snippets of code, expectations, etc, that once one completes make on the extension make test will execute the test and show test results. You write these tests through the .xml described above.

Comparisons of Three PHP MVC frameworks.

PHP is fast becoming one of the dominant languages in the web. It is easy enough for beginners, but has significant power for serious sites. Model View Controller (MVC) frameworks which have been used in many different contexts for desktop applications, mobile phone achitectures and web sites have become a significant force. Java’s Spring Framework  is great in Java and ported to C#. In PHP there are a number that are work considering, some key ones are Zend Framework, by the developers and maintainers of the PHP interpreter, Cake PHP, Code Igniter, and Symphony. I have been working with Zend professionally at work, and have been using Cake and Code Igniter in some personal projects, though have not worked with Symphony. I am going to discuss comparisons of the first three.

Cake PHP is a powerful strongly MVC standards based framework. It has a popular following, and a lot of nice features. If your application can fit into the strict cake pattern, it works well, when you can’t work within the framework’s expectations, it can be challenging and frustrating. This is similar to Spring in Java. Understanding the framework well enough to know how to implement your design is important. One challenge is how tightly coupled the database structure and the model is, including field and table naming. It also has substantial ORM features featureSimple joins where difficult, requiring unnecessary association tables for a simple project. Scripts are used to manage the application dealing with the mappings and creating objects. Cake is in active development and has a strong following, including fanatics.

Code Igniter is a lighter more flexible framework, with base classes that are highly optimzed and kept light by comparison, though you can add complexity as you need it. It has a strong user base and is in active development. For a smaller project, this is a good match. The coupling between model and database are looser. It also has the ORM mapping.  The model can be simple or complicated as necessary. A big project with a lot of complexity, may not be a good fit, but it has good features. Development is pretty fast. Php templating is standard, though a template system is integrated as a feature.

Zend Framework is a framework that is one of the best, but with limitations. I have been using it to build a large REST middle tier that to extend xmlrpc services provided by the Openx Adserver. It has features for many types of technologies, including integrating STOMP JMS features, web services, ajax,  json and front end view functionality. It has some decent database technology which  can work with more databases than just mysql. It uses a modern php style reminiscent of java, including interfaces, abstract base classes, access levels i.e. public, private, protected. It uses a substantial number of design patterns. Class naming is somewhat strict, though by doing so, gives wonderful autoloading features, so no include_once code just construct an object. Zend does not have the strong ORM features, though users have intergrated the Doctrine modeling framework. Others have also included the Ezcomponents framework which can help to do nice features. Php views are standard, though it’s easy to integrate smarty templates. An additional challenge is the structure of the app has changed frequently and technology has been added and improved so some documentation you may find from other sources may be out of date.