International Developer Logo Last Updated 17.05.08 at 11.48
On Sale
This months front cover, click to see the table of contents.
Subscribe
 
TUTORIALS

Developing Three-Tier Web Applications with PHP


Daniel Winter   01.10.05

The Database Abstraction Layer
In order for an application to be portable between different database vendors, it must be blissfully unaware of the particulars of the database it relies upon. This requires a database abstraction layer that wraps-up all of the vendor-specific code into generic functions and objects. By developing a single data access library, you can port the entire application to another database type without having to modify any core functionality. Instead, you simply code an interface module for the particular database in question. For instance, you may be developing an application using MySQL but the final production deployment environment uses Oracle. With a database abstraction layer, this won't be a problem so long as you have libraries for both vendors and PHP does.

Another reason to abstract the database interface is to separate the application into task-specific modules. This becomes particularly important when dealing with applications that are likely to encompass hundreds or thousands of pages. When a lot of individual web pages are involved, there are instant benefits from creating reusable code. Secondly, by extracting common tasks from each page, the code becomes easier to navigate, read and manage. Thirdly, by abstracting database specific logic, you can leave the core PHP pages to perform application-specific logic. This means that if some business rules change you know the application files have to be modified. If the database design, type or version changes, however, you know to go straight to the database libraries instead.

When approaching database abstraction, there are a couple of roads you can take. Which route you take depends largely on whether all the databases you intend to support in your application use compatible SQL syntax or not. Often an application will be designed to use a single type of database, in which case you can often embed SQL queries within the application logic, leaving the database libraries to manage low-level operations such as connections instead. Alternatively, for pure data abstraction, you can opt to use a data access objects as well as data query objects. This requires significantly more code and a little overhead, but it means your application is completely free from SQL queries altogether. 


The Application Logic Layer
The business logic is contained within the application layer. While most of the database layer PHP files are implemented as include files, the PHP files that make up the application layer are generally the ones that are requested by web browsers. They take requests from the presentation layer, pass them to the data layer and then process the results according to the rules of the application.

The results are then fed back into the presentation layer for formatting. It is also worth noting that by separating the application layer from the presentation code, the application becomes independent of the client infrastructure. This means the application can be given additional interfaces for clients other than web browsers. For instance, mobile phones, PDAs and desktop software are common frameworks for delivering alternative user interfaces.




   Previous Page  1 2 3 4 5 6 7 8 9 10 ... Next Page   

HAVE YOUR SAY
This article is rated  Rate this article