Sep 30

About PHP applications and OOP

Filed under: Development | Taged as: , , | Comments Off

PHP5 new OOP features now allow programmers to create a real object and class based application. But there is one limitation that complicates this development: web based applications rely on a request-response cycle and unlike JSP, PHP does not keep object states between different requests (not talking about hack solutions including sessions or special server extensions). The result is, that objects must be initialized and restored everytime they are needed (maybe based on data stored via GET or POST). So, PHP does not really seem appropriate for developing real OOP applications, beside the use in code libraries.

On the other hand, if you look at the typical CRUD actions you have within a database based system, you possibliy could encapsulate the fields in a single table in a object. This results in a cleaner code because the access and manipulation of the fields always includes the object.

An important design aspect for software (and also webbased applications) is the seperation of logic and presentation (database actions and HTML output). A typical CRUD application provides a solution per definition. Insert, update and delete actions are used for the business logic while the select data must be displayed. A little twist and tweak of the MVC Pattern, the knowledge about the request and response cycle from the first section and the interpretation of database fields as objects result in the following situation:

  • Database fields a represented by an object.
  • Datamanipulation is manged with this object by a controller.
  • The state of the database is displayed by a view.
  • Queries are encapsulated in class methods, acting on the data objects.

Datamanipulation in an web environment is usually initiated by a GET or POST variable, evaluated by a script which runs all the necessary functions and queries. The Controller will make this job and handle all the request data. A view will then show the current state of the database, providing the HTML-output.

A typical cycle in this model might look like this:

  1. The controller is requested with GET or POST data.
  2. Based on this data an object is created representing the current state.
  3. The contoller decides what action to perform an passes the object to the necessary function(s).
  4. The logis is executed.
  5. A view selects the data it needs and displays the output.

Comments are closed.