views:

198

answers:

3

What are the main components of a PHP application? I've come up with these:

1) Database

2) HTML templates

3) Sessions/Cookies/Authentication

4) User Input ($_GET or $_POST or URL segments)

Are these the main components or are there any others?

The reason I'm asking this is so I can put each 'Object' in its own class without having to worry about how it will be technically feasible. For example, I can have a Post class which allows you to add posts to a blog, without having to worry about how it will fit in with the rest of the system / php language, etc.

A: 

I would add a data access layer as a component that provides the interface to the database.

There are existing patterns that take care of those types of concerns. And there are frameworks that are built upon those patterns that you might want to take a look at, where much of this work has already been done:

Cake PHP

Solar PHP

Turnkey
A: 

It sounds like you want some kind of model-view-controller framework. A framework following that pattern encapsulates all the DB work into models, the business logic into controllers, and the presentation/templates into views. Two frameworks come to mind:

  1. CodeIgniter (personal favorite)
  2. Cake PHP

If you go that route you'll find that MVC frameworks will help you to separate the data as you're describing. Plus those systems tend to have a lot of plugins or built-in functionality that will further encapsulate things like authentication, session management, post/get data, etc.

Parrots
If you spend much time studying the proper design of object relational mapping layers and the application domain layer you'll find that the model is not the database interaction layer and that business logic belongs in the model not in the controller.
Noah Goodrich
+2  A: 

This sounds a lot like this question.

I would strongly recommend that you take a look at Patterns of Enterprise Application Architecture by Martin Fowler.

I would also recommend that you search for questions on this site related to the Model or Domain as well as Object Relational Mapping or Database Abstraction. I happen to know that there is a great deal of excellent content particularly in regard to PHP.

I see two questions in this question that you've posted. First, what are the general architectural components of a site. Generally you'll have these three in some manifestation:

  1. Database and Database Interaction Layer
  2. Controller - handles $_GET and $_POST (the request) and assigning content to the View and ultimately rendering it.
  3. View - should contain only HTML and very basic code such as loops for iterating over collections and variable output.

Second question I see is where to place handling of a specific business object in the application. This is where the discussion gets a little more involved because I assume that you need to interact with Posts both as business objects (within the domain) and as rows in a database table. Both of these concerns can be wrapped inside of the same class utilizing a pattern called Active Record which has been popularized by Ruby on Rails. However, depending on the complexity of the application and database involved you may want to consider separating the business logic from the database interaction by creating one Post class that acts as the database interaction layer and another Post class that contains all of the business logic.

Noah Goodrich