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.
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.
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
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, 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
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
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.
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.
Links:
[1] http://systeminetwork.com/author/erwin-earley
[2] http://systeminetwork.com/files/62653-Fig1.gif
[3] http://systeminetwork.com/files/62653-Fig2.gif