Published on System iNetwork (http://systeminetwork.com)
PHP Frameworks
By bradforde
Created Oct 24 2008 - 14:46

Frameworks can save your development team valuable time and energy
By:
Erwin Earley [1]

Ah, PHP. Originally designed for producing dynamic web pages, it has evolved so much since its birth over a decade ago. Although you may understand the basics of PHP, you could be missing a vital component of PHP development: frameworks. Frameworks can help development teams concentrate on specific requirements without expending valuable time and energy designing and implementing low-level, common functions required by an application. With frameworks, a team can develop projects much faster than if they were writing all of the code from scratch.

A software framework is basically an application model customized by a developer to satisfy a particular requirement. Frameworks provide a rich set of libraries for commonly invoked tasks, as well as simple interfaces and logic structures for accessing those libraries. They typically contain two main parts: source code for solving particular problems and APIs that call specific functions within the framework.

Many software frameworks are available for PHP, and you can use them to ease your development tasks and integrate advanced functionality into your web applications. (Keep in mind that PHP on i is the same as PHP on any other platform, so frameworks available for PHP will work with your PHP applications on i.) I'll show you how.

Comparing Frameworks

More than 40 frameworks are currently available, and more are released every day, it seems. Which framework is best suited for you really depends on a variety of factors, including the requirement you are trying to satisfy and the coding style you prefer. The table in Figure 1 [2] summarizes some of the features of the 10 most popular frameworks available for PHP.

Following is an explanation of each support item. All of these frameworks support PHP 5 — the version of PHP that runs on i.

  • MVC: The framework supports the Model-View-Controller (MVC) architecture, which breaks an application into three parts: the model, the view, and the controller. I'll explain more about MVC in a moment.
  • Multiple databases: The framework supports multiple databases without requiring changes.
  • Database objects: The framework includes additional database objects.
  • Templates: The framework has a built-in template engine.
  • Caching: The framework supports caching — typically object caching.
  • Validation: The framework has built-in support for validation and/or filtering.
  • Ajax: The framework has built-in support for Ajax.
  • Authentication model: The framework has built-in support for user authentication processing, such as Access Control Lists (ACLs).
  • Modules: The framework supports other modules, such as RSS feed parsers, PDF processors, and Google data.

Model-View-Controller Explained

Before I talk about specific frameworks, let me take a step back and define MVC architecture a bit further. MVC isolates the business logic of an application from user interface considerations, so a developer can modify one without affecting the other. The three components of MVC consist of

  • Model: application data and the business rules used to manipulate the data
  • View: elements of the user interface
  • Controller: user actions that communicate to the model (e.g., keystrokes or mouse movements)

Figure 2 [3] represents the relationship between the three components of MVC. The solid lines represent a direct relationship, and the dashed lines indicate an indirect relationship.

Zend Framework

Zend Framework, which is available from framework.zend.com or with the purchase of Zend Core for i (at zend.com), is an open-source framework for developing web applications in PHP 5. Zend Framework is both a component library (because it provides standalone, loosely coupled components) and an MVC framework, which can help establish the basic structure for a Zend Framework application.

Some of the components in Zend Framework include

  • Zend_Acl provides ACL functionality and privilege management.
  • Zend_Config simplifies the use of configuration data for web applications.
  • Zend_Controller and Zend_View provide the infrastructure for MVC websites.
  • Zend_Feed offers a simple way to work with live syndicated feeds.
  • Zend_Filter and Zend_Validate include basic filtering and validation tools for developing secure websites.
  • Zend_Gdata provides read/write access to services hosted at google.com, such as Spreadsheets, Calendar, Blogger, and CodeSearch, by using Google Data APIs.
  • Zend_Mail and Zend_Mime create and send email messages.
  • Zend_PDF creates, reads, and modifies PDF documents from PHP applications.

Let's take a brief look at the Zend_Pdf module, which offers a PDF manipulation engine. By incorporating this module with a web application, you can dynamically prepare documents in PDF format, enabling you to

  • create a new document or load an existing document
  • retrieve a specific revision of a document
  • manipulate pages within a document (e.g., changing the page order, adding pages, removing pages)
  • draw primitives (e.g., lines, rectangles, polygons, circles, ellipses, sectors)
  • draw text with 14 built-in fonts or with custom TrueType fonts
  • rotate images
  • draw images
  • incrementally update PDF files

Now let's examine how you can use Zend_Pdf to create and load a PDF document. Here is a snippet of PHP code that uses Zend_Pdf:

/* Include the Zend_Pdf module */
require_once 'Zend/Pdf.php'

The require_once statement will invoke the Pdf.php file, which contains the complete definition of the Pdf module — including the class definitions and function call wrappers. The require_once statement indicates that the file should be included only once. Other requests that include the file will fail.

Now that the program has the Framework definition, we can create a new, empty PDF document:

/* Create a new PDF document */
$pdf = new Zend_Pdf();

The variable $pdf represents an instance of the class Zend_Pdf, which is provided by the framework. The keyword new indicates that a new instance of the class should be created, so the variable $pdf can now load new pages into the resulting PDF document.

To create a PDF document from an existing file, use the command

/* Load a PDF document from a file */
$pdf = Zend_Pdf::load($filename);

Likewise, to load a PDF document from a string, use the command

/* Load a PDF document from a string */
$pdf = Zend_Pdf::parse($string);

All three of the above calls (i.e., new Zend_PDF, Zend_Pdf::load, and Zend_Pdf::parse) return a new Zend_Pdf object you can use to manipulate and save the resulting PDF document.

Now that the program has a variable that represents a PDF document ($pdf), let's look at some of the methods for manipulating and saving the document. In the code statement

/* Add a new page to the PDF document */
$pdf->pages[] = $pdf->newPage
(Zend_Pdf_Page::SIZE_A4);

$pdf->pages refers to a public member of the Zend_Pdf class — in other words, $pages is an array in the $pdf variable that represents the pages within the PDF document. You can manipulate this array as you would any other. Referencing the index without an index item (by using []) adds the new page to the end of the array. Zend_Pdf_Page refers to an object within the Zend_Pdf framework, and the $pdf variable refers to a Zend_Pdf object containing an array of Zend_Pdf_Page objects (representing the individual pages of the document).

We could have accomplished the same outcome with the following call:

/* Add a new page to the PDF document */
$pdf->pages[] = new Zend_Pdf_Page
(Zend_Pdf_Page::SIZE_A4);

The following code would remove page 5 from the PDF:

/* Remove a page from the PDF */
unset($pdf->pages[5]);

And this command saves the PDF in the file indicated by the $filename variable:

/* Save the PDF as an existing file */
$pdf->save($filename, true);

Note that the true parameter (which is optional) will overwrite the file represented by $filename, if it exists.

You can also omit the parameter specification and save the file as a new file:

/* Save the PDF as a new file */
$pdf->save($filename);

With this method, the PDF document will be saved in the file indicated by the variable $filename — as long as that file does not already exist. If the file does exist, then the call will fail.

Zending You on Your Way

Now that you've seen how to use frameworks to integrate advanced functionality into your PHP applications and speed your PHP application development, know that there is much more that you can do with Zend_Pdf as well as the other components in Zend Framework (and for that matter, other frameworks available for PHP). To learn more, start by viewing the examples provided in the Zend Framework Programmer's Reference Guide (framework.zend.com/manual/en).

Erwin Earley is a staff software engineer at IBM and has worked with the Rochester, Minnesota, development lab since 1996. He has worked in the IT industry since 1980 and has experience with several Unix variants as well as Linux and OS/400.

© 2010 Penton Media, Inc.

Source URL: http://systeminetwork.com/article/php-frameworks

Links:
[1] http://systeminetwork.com/author/erwin-earley
[2] http://systeminetwork.com/files/62653-Fig1.gif
[3] http://systeminetwork.com/files/62653-Fig2.gif