Zend Framework Review

zend-framework.gifEarlier this week, I gave the latest version of the Zend Framework v-1.9.2 another test drive. I had previously dabbled in v-1.7.4 as well as a pre-1.0 incarnation of the framework. I will not repeat listing the whole breadth of its functionality here, since you can find this elsewhere on the Internet. Neither will I present a point-by-point analysis, just the salient points, short and sweet, which you can expect to be coloured by my personal view.

Suffice to say that the ZF (Zend Famework) is based on MVC -you’d never guessed- and it provides functionality for database access, authentication and access control, form processing, validation, I/O filtering, web services access, and a bunch of other things you would expect from a web framework. The first thing to notice is that the framework has grown up and I mean this quite literally from a few megabytes in its early days to a whopping 109 MB (unzipped) distribution package. Only about 21 MB are used by the framework itself; the rest contains demos, tests, and… the dojo toolkit… an old acquaintance, which is optional.

The documentation for the ZF was excellent right from the beginning and it has staid that way. Included is a 1170-pages PDF file, which also bears testimony to the growing size and complexity of the framework. Gone are the days when one could hack together a web application without reading a manual. One of the first things to realise is that ZF is glue-framework rather than a full-stack framework. This means, it feels more like a library or a toolkit. ZF does not prescribe architecture and programming idioms like many other web frameworks do. This appears to fit the PHP culture well, though it must be mentioned that most ZF idioms come highly recommended, since they represent best OO practices.

Another thing that catches the eye is the lack of an ORM component, which may likewise be rooted in traditional PHP culture. If you want object mapping, you would have to code around ZF’s DB abstraction and use Doctrine, Propel, or something similar. Let’s get started with this item.

Database Persistence
ZF provides a number of classes for DB abstraction. Zend_Db_Table implements a table data gateway using reflection and DB metadata. You only need to define table names and primary keys. Zend_Db_Adapter, Zend_Db_Statement and Zend_Db_Select provide database abstraction and let you create DB-independent queries and SQL statements in an object oriented manner. However, as you are dealing directly with the DB backend, all your data definitions go into the DB rather than into objects. Although this matches with the traditional PHP approach, it means that you need to create schemas by hand, which may irritate people who have been using ORM layers, like Hibernate, for years. On the other hand, a full-blown ORM layer likely incurs a significant performance cost in PHP, so maybe the ZF approach is sane.

Fat Controller
Like many other frameworks, ZF puts a lot of application logic into the controller, and this is my main gripe with the ZF. It seems to be the result of the idea that the “model” should concern itself only with shovelling data from the DB into the application and vice versa. A case in point is the coupling between Zend_Form and validation. This leaves you no option, but to put both into the controller. I think that data validation logically belongs to the model, while form generation logically belongs to the view. If you pull this into the middle, it will not only bulge the controller, but it is likely to lead to repetition of validation logic in the long run. That’s why I love slim controllers. Ideally, a controller should do nothing but filtering, URL rewriting, dispatching, and error processing.

MVC Implementation
Having mentioned coupling, it would do ZF injustice to say that things are tightly coupled. Actually, the opposite is the case, as even the MVC implementation is loosely coupled. At the heart you find the Zend_Controller_Front class which is set up to intercept all requests to dynamic content via URL rewriting. The rewriting mechanism also allows user-friendly and SEO-friendly URLs. The front controller dispatches to custom action controllers implemented via Zend_Controller_Action; if non-standard dispatching is required this can be achieved by implementing a custom router interface with special URL inference rules. The Zend_Controller_Action is aptly named, because that’s where the action is, i.e. where the application accesses the model and does its magic. The controller structure provides hooks and interfaces for the realisation of a plugin architecture.

Views
Views are *.phtml files that contain HTML interspersed with plenty of display code contained in the traditional <? ?> tags. It should be possible to edit *.phtml files with a standard HTML editor. The Zend_View class is a thin object from which View files pull display data. View fragments are stitched together with the traditional PHP require() or with layouts. It is also possible to use a 3rd party templating system. Given the <? ?>, there is little to prevent application logic from creeping into the view, except reminding developers that this is an abominable practice punishable by public ridicule.

Layouts
Layouts are a view abstraction. They enable you to arrange the logical structure of page layouts into neat and clean XML. These layouts are then transformed into suitable output (meaning HTML in most cases). As you can probably infer, this takes a second parsing step inside the PHP application, which is somewhat unfortunate, since PHP itself already parses view components. While layouts are optional, they are definitely nice to have. I think it’s probably the best a framework can do given the language limitations of PHP, which only understands the <?php> tag. If the XML capabilities of PHP itself would be extended to process namespaced tags like <php:something>, then one could easily create custom tags and the need for performance-eating 2-step processing would probably evaporate. Ah, wouldn’t it be nice?

Ajax Support
ZF does not include its own Javascript toolkit or set of widgets, but it comes bundled with Dojo and it offers JSON support. The Zend_Json class provides super-simple PHP object serialisation and deserialisation from/to JSON. It can also translate XML to JSON. The Zend_Dojo class provides an interface to the Dojo toolkit and makes Dojo’s widgets (called dijits) play nicely with Zend_Forms. Of course, you are free to use any other Ajax toolkit instead of Dojo, such as YUI, jQuery, or Prototype.

Flexibility
As mentioned, ZF is very flexible. It’s sort of loosely coupled at the design level, which is both a blessing and a curse. It’s a blessing, because it puts few restrictions on application architecture, and it’s a curse, because it creates gaps for code to fall through. A case in point is dependency injection ala Spring. In short, there isn’t much in the way of dependency management, apart from general OO practices of course. Nothing keeps programmers from having dependencies floating around in global space or in the registry. A slightly more rigid approach that enforces inversion of control when wiring together the Zend components would  probably not have hurt.

Overall Impression
My overall impression of the ZF is very good. It is a comprehensive and well-designed framework for PHP web applications. What I like best about it that it offers a 100% object-oriented API that looks very clean and makes extensive use of best OO practices, such as open/closed principle, programming to interfaces, composi
tion over inheritance, and standard design patterns. The API is easy to read and understand. The internals of its implementation likewise make a good impression. The code looks clean and well structured, which is quite a nice change from PHP legacy code. ZF still involves a non-trivial learning curve because of its size. I’ve only had time to look into the key aspects, and didn’t get around to try out more specialised features like Zend_Captcha, Zend_Gdata, Zend_Pdf, Zend_Soap, and web services, and all the other features that ZF offers to web developers. If I had to choose a framework for a new web application, ZF would definitely be among the top contenders.

Zend Studio for Eclipse

Usually I don’t talk about new product announcements in this blog, because there are just too many of them and often they are only of interest to a small group. Yet, I think the recent release of Zend Studio for Eclipse (Beta) will be exciting news for almost every PHP developer. Last year I had evaluated the previous version of Zend Studio, which is certainly excellent. However, I’ve grown attached to the Eclipse IDE, and therefore I did not make the switch and kept using the PHP Eclipse plugin instead. It seems that Eclipse has gained popularity across the board in the PHP world. Zend has recognised the signs of the time and released an Eclipse version of its flagship product earlier this month. This puts Zend Studio into direct competition with PDT and PHP Eclipse.

The Zend IDE has all the usual niceties, such as code assist, syntax colouring, code fold, class hierarchy view, property inspectors, auto format, and powerful search/replace functions without which most PHP developers probably cannot live any more, but there is even more. In addition to PHP code editing, the IDE also provides JavaScript and CSS editors. I have always wished for the sort of code refactoring functionality for PHP that you get with the Eclipse Java editor. The new Zend Studio finally offers this, although perhaps somewhat less extensively than what Java programmers are used to. At least there’s support for renaming and moving, as well as automatic includes. This alone should be a huge time saver, especially for larger projects with dozens or even hundreds of source files.

Further high-end features include support for PHPUnit Testing, PHPDoc generation, as well as debugging and profiling. I cannot stress enough how important these features are, especially when working in a team. The PHPUnit support generates skeleton test classes for all code elements and thus finally takes the pain out of creating meaningful test suites. Well, the programmer is still responsible for making them meaningful, but there isn’t so much typing involved any more. Likewise, with full PhpDoc support there is finally no more excuse for not including the programmer documentation in the sources. Project managers will probably welcome this, just as the felicitous integration of CVS and Subversion which works right out of the project view. Given “var_dump” and logging I have rarely felt the need for a debugger, but since debugging has always been a strength of Zend Studio, one might welcome this as an additional luxury. The profiler, on the other hand, can prove to be vital when checking large chunks of unfamiliar code for weaknesses.

Other noteworthy features are support for code templates and snippets (probably great for lazy typers with a good memory), inclusion of the open-source PHP/Java bridge which allows using Java classes from PHP and vice versa, and a number of wizards that help to get projects and classes getting off the ground faster, including support for WSDL/SOAP. Perhaps even more noteworthy are the integrated SQL GUI editor and HTML WYSIWYG editor. The SQL GUI offers the typical DB server explorer tree, table data viewing and editing in grids, a query editor with syntax highlighting and BLOB views. This is certainly helpful for casual database development, although “serious” DB developers might still prefer the respective DB manufacturer’s tools in combination with an ORM library. The HTML WYSIWYG editor is another really big addition. It looks a bit like an early version of Dreamweaver and it offers code, design and split-window code/design views. I doubt that it can replace a high-end designer tool, but it is certainly more than sufficient for the typical web application, and perhaps it even allows less artistically inclined programmers to add some visual polish to their pages.

In summary, Zend Studio for Eclipse is a high-end development tool with many advanced productivity features. The product is offered at a price of $254 USD and the final release is announced for the end of 2007. Further information, including a number of demo videos, is available at the Zend website.