rick_grehan
Contributing Editor

Fabulous PHP frameworks: Zend Framework

reviews
Jan 26, 20117 mins

The Zend Framework offers a comprehensive feature set and popular components in return for a significant time investment

Rusty steel girder and two meter web installed on a bridge abutment and scaffolding as part of a new freeway interchange. Building construction infrastructure project.
Credit: Randy Hergenrether / Shutterstock

The Zend Framework, sponsored by Zend Technologies, is a comprehensive framework loaded with features. Currently at Version 1.11.2, it requires PHP 5.2.4 or later and makes full use of PHP 5’s object-oriented features.

Zend Technologies also produces the Zend Server PHP application server. And while Zend Server is not required for Zend Framework, running Zend Framework applications on the Zend Server provides substantial benefits. First, Zend Framework provides a Zend_Log writer that writes to the Zend Server’s monitor API, allowing you to use the Server’s log introspection feature for drilling into application issues. Also, the Framework includes a Zend Server-specific Zend_Cache back end for caching resource-intensive operations, such as database results, Web service calls, complex templates, and more.

While Zend Framework supports the MVC (model view controller) design pattern, and most of the tutorials and examples employ that pattern, Zend does not lock you into its use. You can use MVC “pieces” instead. It’s possible (though not necessarily optimal) to create a Zend Framework project using only view components. In fact, the Zend Framework’s construction is modular enough that some PHP frameworks suggest employing its components for features they do not support directly.

Zend Framework uses the “front controller” pattern to manage responses to Web requests. (Most of the other frameworks do the same.) This means that all requests pass through a single index.php file, which acts as a kind of central switching system, routing each request to the responsible controller class. This not only simplifies request handling, but also ensures that the environment is properly initialized before the application performs any actions.

Test Center Scorecard
 
 40%25%25%10% 
Zend Framework 1.11.29798

8.4

Very Good

Zend Framework libraries and tools
Download and unpack Zend Framework, and you get a collection of libraries organized within a directory structure. Zend provides a command-line tool, zf, that jump-starts the creation of specific pieces of an application by generating scaffolding code, placing that code in the proper files, building the proper subdirectory structures, and storing each file in its correct subdirectory. To create an application, you define the application’s Web root directory, then use the zf tool to build the directory structure using a command like the following:

zf create project <project-name>

This creates a directory in the Web root and adorns that directory with all necessary subdirectories.

The zf tool also builds controllers, models, views, actions within controllers, and even data-entry forms. Suppose you’ve defined a database with a table that tracks music albums, and you want to create a skeletal class for accessing that table. You would enter the following:

zf create db-table Albums albums

Similarly, if you’ve already defined an Index controller for your application and want to add a delete action to it, the zf tool will get you started with the following:

zf create action delete Index

In all these examples, the zf tool is building skeletal files, classes, and methods. It’s up to you to hang the flesh on the bones.

A controller’s operations are called actions, each of which is executed by a public function referred to as an action function. Action functions can also be implemented in model classes. For example, the action function that updates an Album within an Albums database must be named updateAlbum().

While the number of directories and subdirectories required by a Zend Framework application might be daunting, this complexity is actually organization. (This is true for most of the other frameworks in this review.) Every subdirectory houses specific application components. Controller source files go in the controllers subdirectory, model source files in the models subdirectory, JavaScript files used by views in the views/scripts subdirectory, and so on. It is truly a case of “a place for everything, and everything in its place.”

In addition, class and method names follow well-defined convention, and this imposition of name structure permeates the Zend Framework. The benefits are twofold: It not only makes the function of a class or method instantly apparent, it also allows the PHP runtime to use introspection to locate which class or method is responsible for a particular piece of functionality at runtime. This latter feature of naming conventions allows the system’s front end to map a particular URL to a given action. The controller peels the URL apart and uses its components to locate which class and which action within the class should handle the HTTP request.

Zend Framework design patterns
The core of Zend’s model subsystem is Zend_Db. Rather than implementing a single design pattern, Zend_Db is a collection of different patterns. At its heart is the adapter pattern, but it also uses the façade pattern to provide an object-oriented interface for building dynamic queries. The Zend_Db_Table subclass of Zend_Db implements the table data gateway pattern, and the Zend_Db_Table_Row class implements the row data gateway pattern.

Zend_Db does not implement an active record pattern (a pattern employed by default in Ruby on Rails). Zend engineers explained they decided not to on account of that pattern’s shortcomings in performance and flexibility. They also decided not to implement an Object Relational Mapper (ORM) in Zend_Db, partly because ORMs are complex and — in their words — “difficult to get right,” and partly because other projects, such as the Doctrine ORM, were already tackling ORM.

The Zend Framework team has partnered with the Dojo Foundation to provide first-class integration for the Dojo JavaScript toolkit. In addition, community volunteers have created tools for jQuery integration that mimic the support and API provided in the Dojo integration.

The Zend Framework’s online documentation includes a programmer’s reference guide, which also incorporates a quick-start introduction for neophytes. Other getting-started guides target specific components of the framework. For example, the website features a starter guide for the Zend_Search_Lucene component, which supports the Lucene full-text indexing and search system. Other resources on the website include a guide for improving application performance using intelligent class loading and an unfortunately brief section that covers database performance enhancements. Finally, the website is home to a collection of nearly 30 webinars on a wide range of topics such as application testing, debugging, security issues, AJAX, and more.

There is much to learn in the Zend Framework. If you’re looking for a comprehensive library of PHP Web-building components, you’re likely to find all you’ll need in Zend. But if you want to get your website off the ground quickly, Zend is probably not your best choice.

Read the reviews of other PHP frameworks: