#native_company# #native_desc#
#native_cta#

Object Orientation

By Mark Williams
on July 30, 2000

Overview

OK, so you’ve heard all about it, you think you’ve got your head around it.
Everyone you speak to says it’s the way real developers develop. How do you do it?
What’s in it for you? And how the hell do you do it with PHP?
Broadly speaking, all of the above statements are true, but I guess if you’re
not familiar with OO you want to see a concrete example – and ideally something, which
you can fit into your own page(s). Well, here goes.
Let’s start off by seeing exactly what we’re aiming for.
[Image:] Preview of Results
Basically, we have three boxes (F1 Teams, Drivers Championship and Constructors Championship).

F1 Teams

Quite simply, this is a box with an un-ordered list in it. Each item is a link to the Team’s web site.

Drivers Championship

This is an informational box, showing the current points standing in the F1 Drivers Championship.
There are no links within this box.

Constructors Championship

Like the Drivers Championship box, this is also an informational box, showing the current
points standing fo the F1 Constructors Championship.
Now, before you say it, I know you probably don’t want details on the current F1 championship,
but this does serve as a sample application. At the end of the day, the data is simply taken from a
database (in this case I am using MySQL), so you can use whatever data you like. The point is these boxes
are “great” portal-type tools for showing lots of focused data. Whether you are a fan of them or not, they
do work.

How do we create them?

In my mind there are a number of ways we can create these:
  1. Hard code the data into a web page. There is nothing wrong with this method if you
    just want the box(es) to appear on one page, but once you start spreading the across pages, the administrative
    overhead does become tedious.
  2. Set the boxes up as a server side include file. Again, there is nothing wrong with
    this; you can then include the box(es) on any number of pages simply by referencing the include file.
  3. Create the boxes as Objects. Initially, this does take a little longer to build,
    but it does make for portable code (I’m talking about code which walks from site to site, not just page to
    page!). Additionally, we create interfaces to both the data source and the layout of the box(es), which
    means we have “easier” control of their data and layout.
As you’ve probably guessed, we’re going with the OO method. What would be the point otherwise?
So, let’s start at the top…
Setting the requirements. For the purpose of this exercise, I’m going to define my requirements as:
  • Data source must be variable and controllable.
  • Layout dimensioning must be variable and controllable.
  • Colour must be variable and customisable.
  • Font Face must be controllable.
There are a number of ways we can meet these criteria; and for the latter two, it is probably
easiest to make use of CSS, which is what we’ll do. As for the first two, so the story begins:
To build this box we are going to use 7 separate files:
  • index.phtml
  • mysqldb.obj
  • infobox.obj
  • linkbox.obj
  • resultbox.obj
  • constants.inc
  • main.css
Standards
All .obj files are class declarations. I use one file per class/subclass. The .phtml file is
the file we are gong to insert our box(es) into. The .inc file is a generic server side include file
and the CSS file is exactly that – a Style Sheet.

1
|
2
|
3
|
4
|
5
|
6
|
7
|
8

Download: mark20000727.zip