N-tier has (potentially) alot to offer, particularily in terms of saving developers that most important of resources: time...
But there are many different points of view on what “n-tier” means and what actually constitutes a tier in a layered application. Its further confused by the big software houses describing n-tier in different ways. In the PHP circles, n-tier seems to have many interpretations. This is mine...
N-Tier: What's the point
Software developers are forever in pursuit of more time. The principles of N-Tier design are one mechanism to get there. N-Tier is about building distinct layers into applications so that maintenance and development becomes a concentrated effort within a particular layer. N-tier can become like the division of labor Henry Ford introduced to his Model T car factories, database designers, business software analyists, web developers and web designers, all working together on the same overall system while being able to act independently of one another.
N-Tier is also about constructing distributed systems, allowing many parties to interact with what is ultimately the same environment, while getting the most out of your network. If you were Amazon for example, with a web presence all over the world, you might choose to deploy a single database in the states then web servers around the world, all of which end up communicatng with the same database. The links between the servers and the core systems (such as the database) use high speed, private networks, while the general public connect to their local country web server over a regional, rather than international ISPs.
The other thing that may happen in N-Tier architectures is multiple platforms, be they mainframes, Unix, Windows based or whatever are coupled together to form a complete “whole”.
A 5 Tier example
It’s important to note the the N in N-Tier means “insert your own number here”. There’s no hard and fast rules as to how many tiers to use or what each tier should actually do. Having said that, here’s a common example which is pretty much what Sun and Microsoft are encouraging with J2EE and .NET right now;
<list> Presentation GUI: [e.g a web browser] parses HTML, WML, XHTML etc. to deliver a human friendly user interface. Receives the “raw” content from the Presentation Logic tier using HTTP. Presentation Logic: [e.g PHP on a web server] renders HTML, WML etc. and delivers it to the Presentation GUI over HTTP. Receives data to “bind” with the content it is rendering with data from the Business Logic tier. Perhaps uses SOAP/XML-RPC client to fetch the data to bind from the Business Logic tier or even the Java EJB API. Business Logic: [server running EJBs, COM objects, CORBA or utilising XML messaging (e.g SOAP)] manipulates and transforms data passed to it from the data access tier according to business rules. In other words, the business logic tier “adds value” to the data sent from lower layers. If you have a table of customers stored in a database, the business logic tier might add the value of preparing a list of customers in a particular geographic local with an income above a some figure. Frequently the business logic and data access tiers will be found on the same physical platform but in large systems may be remote, communicating over a network. Data access tier: [e.g server running JDBC] all data I/O to data repositories handled here for example connects to database, fetches data from various sources including files, XML documents, spread sheets and so on. Data tier: [e.g a database] application data is stored here in repositories - perhaps a database, perhaps text files, perhaps even an XML database like the Apache Groups XIndice</list>
It’s important to note that the three middle layers are acting as both clients and servers, being clients to the layer below them and servers to the layer above.
This five tier model is in contrast to the 2 tier client / server model which has been common throughout the ‘90s in corporate environments. In a 2-tier architecture, you often have say a Windows based client implementing everything from the presentation GUI tier all the way down to the data access tier. Ask any company what happens when they want to upgrade their client applications, or what happens when 2000+ clients start hammering a database and you begin to see the point of introducing more tiers.
N-Tier Principles
OK, that’s all very general but here’s my attempt to lay down some rules that should apply if a system is to be described as N-Tier;
- Each layer must <i>be able to</i> exist of a physically independent system. It is not a requirement that each layer <i>should</i> exist on a separate system but rather by making is possible that a layer can be distributed elsewhere, the architecture as a whole is completely scalable.
- Each layer should exchange information only with the layers above and below it. For example, the presentation logic tier must not exchange information directly with the data access tier but instead with the business tier.
- Each layer should be interchangeable. This results in some further rules;
- Each layer should have a clearly defined data interface (API: application program interface).
- Layers should expect nothing of other layers except that they use the defined APIs to exchange data.
The data access tier, for example, should not expect a particular database, such as Oracle, to reside below it. In other words it should be possible to replace Oracle with an alternative database without any impact on the data access tier (or any tiers above it).
5-Tier and PHP
There’s essentially two ways we could go with PHP in N-Tier type systems.
Pure PHP
The first is to try to implement everything in PHP, perhaps even writing our own PHP data repository and a PHP-GTK web browser. In practice we’re really only going to implementing the data access, business logic and presentation logic tiers in PHP. This approach may acceptable in pure “web” businesses and small operations.
With each layer we consider enabling it with an XML messaging technology so that it is capable of residing on a remote system or perhaps consider the MSession extension or Vulcan Logic's Script Running Machine. When considering XML technologies like XML-Schema, XSLT and X-Forms, it becomes apparent that PHP‘s role in might be best applied to offering “roads” by which XML can travel on it’s way from a database to a web browser (or otherwise). So a Pure PHP N-Tier system might look like;
In practice this might equate to something like this;
Will be interesting to see what Yahoo comes up with in the end...
PHP as Presentation Logic
The second option is to regard PHP as the perfect tool for the presentation logic tier, using it’s Java, COM and .NET extensions to natively integrate with those environments (see (*deprecated link* ) as well as using XML messaging like SOAP and XML-RPC (see (*deprecated link* ) to communicate with systems using those protocols. This could be something along the lines of;
For enterpises looking for a cheap, solid, flexible and reliable means to construct their web presence, using PHP in this way could well be the ideal choice, over expensive application servers, vendor lock in and closed source solutions.
Object Orientation and N-Tier
Although N-Tier is not saying anywhere “Thou shallt write OO code” in practice building an N-Tier application without applying the principles of OO design is a nightmare.
In the same way we define an API for a class, so “client” code only needs to be concerned with using the API, not what’s going on behind it, N-Tier layers also have APIs, perhaps defined by a WSDL document (for SOAP) or otherwise.
Templates?
It’s a commonly held belief that using templates with PHP (e.g. Smarty or otherwise) instantly makes you application “N-Tier”. Although templates could be used in an N-Tier environment, in general I don’t believe that using a template system actually adds real layers to you application. The “test” is perhaps this: does modifying your templates require you also modify the PHP code which “parses” the tempalate? There’s a longer discussion of templates in Templates and Template Engines as well as some ideas about what a “View” is The MVC Pattern.
Resources
Develop n-tier applications using J2EE Microsoft's: An Introduction to the Duwamish Books Sample Application The N-Tier.com Internet Portal


