Reprinted PHP Application Framework Design: 1 - Getting Started

Transfer from http://www.lbsharp.com/wordpress/index.php/2005/10/13/php-application-framework-design-1-getting-started/

This article describes the design of a complete application framework written in PHP. The reader is assumed to have a working knowledge of PHP. This part of the series describes the scope of the framework and goes over the initial class hierarchy. The following parts cover everything from session handling to creating page templates.

Introduction

I started web application development back in the days of ASP 3.0. Back then, ASP was so deficient that we had to write most of the code in DLLs and include them into the web projects. Sure, it was a bit cumbersome, but IIS was the best platform available for getting a team of VB developers productive. Then ASP.NET came around and fixed many of its predecessors deficiencies so the company where I worked at the time jumped on the bandwagon. Although I didn't realize it at the time ,. NET spoiled me with its ease of developing large applications (without much need for thorough design and planning).

Don't get me wrong this is not an advertisement for Microsoft. While. NET was gaining ground over the Java platform (please don't email me saying how Java is better thats not the point here), there were quite a few developments in the Open Source community as well. Apache has long established itself as the leading Web Server Software and Open-Source PHP became the answer to ASP. By avoiding the overhead of CGI 1 , PHP combines speed and efficiency with the power and Style of Perl. PHP also happens to be cross-Platform and free (as in Beer 2 ) which makes it a no-brainer solution for many projects.

Unfortunately, PHP has its limitations. Although PHP does fix many features that make Perl unreadable, using it for large projects can be somewhat difficult (relative to Java or. NET that is). So what are the possible problems? For starters, large projects involve many developers with wide ranges of skill-sets. For web-projects, you may have people who don't even know how to do anything save design page layouts and make pretty pictures (and yes we appreciate you guys as well). How do you get everyone to work together productively and be consistent? You may say that this is the Team Leaders job but managers can use help too sometimes. Developing in PHP can also be somewhat tedious when you find yourself copy-pasting the same blocks of code (eg to authenticate the user, insert the page template, connect to the database etc). This article will try to resolve these issues.

So whats missing from PHP? Well, nothing actually, if you spend some time to design what you want to accomplish. While platforms like. NET force you into doing things the Microsoft way, PHP gives you the flexibility to do things your way. Use this power wisely or else you will see the performance of your PHP application dwindle down to that of. NET or even Java (heaven forbid). And, while we are on the subject, why not just use. NET since it does so much work for you? That question is left up to the reader. Maybe the choice of using PHP was already made for you (by your management by magazine type boss). Maybe PHP is the only tool installed on your server. Or maybe you just like the syntax. I really don't care and, if you are reading this (as opposed to having this read to you), the choice was probably already made. I also realize that there are some commercial products that provide a canned-framework for you to use that solve all the problems that I described above. I'm not trying to sell you anything just trying to show that you can easily come up with your own framework. Lets get cracking shall we.

Class Hierarchy

I like to start implementing large projects by drawing out the class diagrams and seeing how the components fit together. Luckily, as of version 4, PHP supports object oriented programming with all its bells and whistles. Although this article is written for PHP 4, you can accomplish all of the same results with PHP 5 (which is still somewhat bleeding edge at the time of writing this). Object oriented programming and design is beyond the scope of this article so I hope you are at least somewhat familiar with it. The point is that OOP is the key to developing large applications and web-development is no exception.

Update: PHP 5 is now fairly mainstream. Although the framework, as described, will still work, some of the hacks described in this article are no longer needed.

We will want to keep the framework minimal for performance reasons yet scalable and flexible enough to handle any kind of project that you may encounter. To start of, lets agree that the logic for each and every page of you application will be encapsulated in a class . This class will inherit from the system base class, which will handle everything that you may want your application to do. The benefits of this approach are fairly obvious. First, you are encapsulating all of the complex code away from the project developers. Thus , you can rest assured that your new intern will not mess with your system classes (unless you want her to for some reason). All the pages in the application will be more consistent and you will not be copy-pasting the same code into every page. More importantly, if you decide to change some underlying functions of your application, you will not have to edit each page as long as you keep the interface to your system base class consistent.

Now lets define what we want our system base class to do. Here is a basic list of features that I will go over in due time:

  • Establish the database connection
  • Define how the page will be laid out and print the HTML
  • Manage authentication and user session information
  • Define all your applications core features

Here is the framework and all of it's glory:

class_system.php - The base Class which includes the following

| _ constants.php - Application Constants

| _ functions.php - Generic functions

| _ class_user.php - Session handling

| _ class_template.php - Template handling

| _ class_form.php - Form handling

Using this framework, all of your pages should have this structure:

include "include / class_system.php";

class Page extends SystemBase (

/ / Your code here

)

$ P = new Page ();

The idea here is that we want to minimize the time spent on the most common tasks such as creating new pages. In this case, all we have to do is include the base class (which will make sure that all the other necessary dependencies are also included), extend the base class, and simply make an instance of the class to get the ball rolling. This way you can start thinking about what the page will actually do rather than waste time with common tasks.

Configuration Files and Useful Functions

The simplest idea to explain is the need for a file that will contain the applications configuration. We can go with XML or some other even more intricate technology that will inevitably make the framework less efficient but lets start off with the basics. All we really need is a set of Constants that define strings such as the Database connection or Installation Paths. These are strings that we would not want to hunt down within each page of the Application every time the Database password is changed or the Application is moved. The File Constants . PHP is just that. The Settings that I Find most useful are the URL and system Paths to the Application.

On a similar note, there are some functions that we would like to always make available and I include them in the File functions.php . These functions are not made as methods of the base Class because we would like to make them available to ALL the classes of our application (including those that do not extend the base class). I will go over the functions in this file when we need to use them. As a simple example, I include the me () function, which returns the file name of the current PHP file. (VB 6 programmers will remember that me is a reference to the current form).

function me () (

return substr ($ _SERVER ['PHP_SELF'], strrpos ($ _SERVER ['PHP_SELF'],'/')+ 1,

strlen ($ _SERVER ['PHP_SELF']));

)

The Base Class

Now lets dissect the class_system.php File Line by Line. The first Thing that we do is call session_start () which starts a new User session IF none exists or does Nothing otherwise (more on sessions in Part 2 of this series). Then we include all the need libraries. There are two things to note about the way this is done. First, require_once is used which will only include the file once and throw an exception if the file is not available (as opposed to include which would only make a warning and proceed with execution of the page). The second thing to note is that absolute paths are used to include the files which are all in the same folder. This is done because we don't want to have to reconfigure the server to get the application working (by changing the include path used in PHP). Relative paths would not work because the base class will be included from various other pages and the relative path would have to work for each page that includes the base class.

session_start ();

$ Path = dirname (__FILE__);

require_once "$ path / constants.php"; / / defines

require_once "$ path / functions.php"; / / generic functions

require_once "$ path / class_user.php"; / / session handling

require_once "$ path / class_template.php"; / / template handling

require_once "$ path / class_form.php"; / / form handling

The implementation of the system base class is not at all surprising if you consider the way we intended to use it. Since the code is only executed when an instance of the class is created, we put everything that we want to happen into the class constructor . Also, since the base class will not be able to know exactly how to render each page, we make the methods abstract and let polymorphism take care of the rest (ie the derived classes will override the base classes methods.) Here is a list of the methods called from within the constructor:

init () - initialize the page

authenticate () - perform authentication

handleFormEvents () - handle page submits

printPage () - output the HTML

destroy () - destroy page (close DB connection)

Unfortunately, PHP 4 does not enforce many OOP principles (note that PHP 5 has a new and much more robust object model). You can get everything to work, such as inheritance and polymorphism, but it takes some effort and some faith. For example , there is no concept of method protection so all methods (and attributes for that matter) are made public. This is a big no-no when it comes to OOP so a common convention is to prefix all methods that are intended to be private with an underscore (_) and then take it on faith that the users of the class will not call these methods. Another problem is that we cannot declare the class to be abstract (ie we do not want people to declare instances of the base class but rather force them to inherit from it). We can get around this limitation by
including the following lines at the top of our constructor (you can read this article for an in-depth analysis of How this Works). The code checks to see IF the instance of the Class is the base Class and throws an Exception.

if (! is_subclass_of ($ this, 'SystemBase')) (

trigger_error ('Base instantiation from non subclass', E_USER_ERROR);

return NULL;

)

Database Access

The application framework should provide a clean interface to your data regardless of the type of SQL server uses. It is also very desirable to have loose coupling between your application and your database backend. So, for example, if you ever decide to change the location of the database or even change the type of SQL server used, you dont want to have to rewrite all your code. Luckily, someone has already tackled this problem for us and all we have to do is use the PEAR:: DB module, which provides a consistent Interface to several popular Database Servers. Co. can read ALL about it at the PHP Extension and Application Repository and I recommend reading the following PEAR:: DB tutorial as well.

Update: The new preferred database library is PEAR:: MDB2 which is a merge of PEAR:: DB and Metabase libraries. You will find migrating from DB to MDB2 fairly straight forward. There is even a migration guide .

Assuming that the PEAR:: DB module is installed and configured correctly, all we need to have a persistent database connection available in every page is a reference to a PEAR:: DB object. We will create this object as a member variable in the SystemBase Class and Create the Database connection inside the constructor. We already have the necessary Constants to build a DNS String and there is also a function in functions.php that returns a pointer to a new PEAR:: DB object.

function & db_connect () (

require_once 'DB.php'; / / pear db class

PEAR:: setErrorHandling (PEAR_ERROR_DIE);

$ Db_host = DB_HOST;

$ Db_user = DB_USER;

$ Db_pass = DB_PASS;

$ Db_name = DB_NAME;

$ Dsn = "mysql: / / $ db_user: $ db_pass @ $ db_host / $ db_name";

$ Db = DB:: connect ($ dsn);

$ Db-> setFetchMode (DB_FETCHMODE_ASSOC);

return $ db;

)

The function imports the PEAR: DB library if it has not yet been imported, creates a DNS string, and connects to the database. All we have to do now is use this function in the SystemBase constructor as follows: $ this-> db = db_connect (); and we are done.

Optimization

You may think it is a bit too early to discuss code optimization at this point since we have yet to implement most of the necessary features. Code optimization is more of an art and I will not go into a line-by-line analyses of how we can achieve the same effect faster. The point here is that we already can tell what the major bottlenecks will be so lets nip the problem now before its gets out
of hand. The most obvious performance loss will be due to the persistent database connection that we establish for each page. In fact, if you make a few timing test to benchmark the performance of the framework you will see that establishing the connection alone take more time than everything else combined. Although it is a necessary evil and we will have to pay that price for it when using the database, not every page in our application will have to connect to the database. In fact, not every page will need to print out HTML content (ie it may just process the request and redirect). So before we go on lets define a few constants for the base class to indicate that we don't need a database connection or to print the page. You will simply define these constants in pages that need them.

define ('NO_DB', 1) if persistent DB connection is not needed

define ('NO_PRINT', 1) if page does not get rendered

Summary

So far, we have laid the foundation for how our application will behave and how the framework will be used. We established an OOP design for the application, defined some constants and function to expedite routine work, and added database support. Read the next part to see how to manage session data and users.

1. CGI (Common Gateway Interface) was a popular method for using Server side code to generate Dynamic Web pages. Any programming language can be used for CGI as long as it can read system environment variables that are set by the Web Server to provide information about the page request. Perl has been the language of choice for CGI programming because CGI programs involve a lot of text processing. Unfortunately, CGI does not scale well because each page request requires the server to fork a new process to run the CGI program. mod_perl is a way to reduce the overhead by including Perl as an internal Apache module; however, benchmarks show that PHP (which also runs as an Apache module) is still faster.

2. The term "free as in Beer" is used in the Open-Source community to distinguish things that cost Nothing and peoples rights. The term was originally published in this paper.
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of Reprinted PHP Application Framework Design: 1 - Getting Started

  • rails version of the change (1) - rails of the introduction

    Rails framework are at the first time in July 2004, its R & D is the 26-year-old Danes David Heinemeier Hansson. Different from complex Web development framework, Rails is more in line with actual needs and more efficient Web development framework. Ra

  • ROR Study Notes 1

    Installed at the success of ROR environment (yes, I installed InstantRails-2.0-win), the first thing done is to create a simple DB demo, the steps are as follows: 1.Rails Appliction Medium Create New Rails App ... 2. Create Application Framework: rai ...

  • Understanding of Ruby Symbol, Part 1

    Ruby is a completely cross-platform object-oriented scripting language, built on the Ruby-based Web framework Ruby on Rails (ROR) is the field of web development is gaining increasing acceptance. ROR substantial use of the Ruby language features, Symbol i

  • An essential reference resource list of Java

    Since the Java platform since 1995 as a whole began to introduce programming community, and its development has gone far beyond the early Java experts and those who promote the idea of "applet ubiquitous" of that vision. In contrast, Java emerge

  • Design Pattern in EJB Applications

    What is a Design Pattern Design patterns are a set of re-use, most people know, after cataloging, code design experience. For the use of design patterns are reusable code, so that the code more easily understood by others, and ensure the reliability code.

  • Ruby Symbol

    Ruby is a completely cross-platform object-oriented scripting language, built on the Ruby-based Web framework Ruby on Rails (ROR) is the field of web development is gaining increasing acceptance. ROR substantial use of the Ruby language features, Symbol i

  • Real design pattern

    I hate the thought of the time to teach some provide a lot of code, because I think the thought process of acceptance should be a pleasure to read as a novel process, rather than spend a lot of brain power for a certain process details And this world ...

  • servlet specification 2.2

    Introduction: Spring Security watch, when to see more use of the Servlet Listener, filter and so on. Filter done, listener never. Therefore want from the Internet to find a closer look at the servlet specification, go thorough investigation, there is no s

  • The company first entered the company I would like to Optimize SQL project

    I was a time not long into the company of people, but also a person to be graduated, but immediately entered the project team, involved in the development. The project is to make a China Telecom Operation System, is a big system. My so-called develop ...

  • JAVA interview questions

    JAVA interview questions 1, object-oriented features of what has 1. Abstract 2. Inheritance 3. Packaging 4. Polymorphisms 2, String data types are the most basic right? Basic data types include byte, int, char, long, float, double, boolean and short. java

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries