Sr. Web Developer
mediabistro.com
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume

Object Oriented PHP
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.
[ Next Page ]


Comments:
RE: Bad practice in this exampleBen11/07/05 08:38
RE: Problem with database variablesThomas Noller01/11/05 15:36
RE: Bad practice in this exampleThomas Noller01/11/05 15:29
Freedom of Choicenick01/06/05 19:14
Fits the real world? A vague claimTablizer12/29/04 23:13
RE: Why stop at procedural programming?edis tamoshauskas11/26/04 10:13
RE: OO logic is king (but PHP OO sucks)Steve L08/18/04 09:52
RE: "Printable" version, well....isn't printaShai06/29/04 17:40
RE: Why stop at procedural programming?Tablizer06/15/04 03:17
Why stop at procedural programming?Pootle05/11/04 11:07
RE: Is this better?acerola04/18/04 00:31
Hierarchies Limited (RE: OO vs. procedural?)Topmind04/03/04 14:34
using 'AND' queries with mysqldb classsandy4door12/18/03 14:13
security aspect - constants.incAlex Haid11/26/03 09:09
RE: No need for Get* and Set*mmj07/02/03 10:20
your script sucksMindLock04/09/03 02:51
even more mistakesgee01/24/03 05:06
This ...gee01/24/03 04:45
OOP Criticism WebsiteTablizer01/10/03 17:18
RE: OO logic is king (but PHP OO sucks)Captain Proton12/19/02 12:53
RE: ConnectionDavide12/19/02 06:16
using classes to make your content portable.Armand Krijgsman11/25/02 05:28
RE: OO logic is king (but PHP OO sucks)Diego11/19/02 22:32
RE: OO logic is king (but PHP OO sucks)Simon62210/25/02 17:11
RE: OO logic is king (but PHP OO sucks)Diego09/29/02 14:18
ConnectionAldrey08/22/02 07:30
Could there be "some" mistakes?C-Man08/22/02 03:58
RE: Perl to PHP ObjectsAnas Mughal08/13/02 14:02
RE: OO vs. procedural?Anas Mughal08/13/02 13:46
RE: return referencesMartin08/13/02 04:42
RE: OO logic is kingRemmelt08/01/02 10:59
RE: OO logic sucksTom Myer07/30/02 01:45
RE: OO logic sucksMike Miller07/25/02 00:23
Global Array???Abhishek Lahare07/12/02 11:11
RE: Object's ArrayEduardo Ramírez06/18/02 01:23
No need for Get* and Set*Stuart06/15/02 15:50
RE: OO logic sucksTablizer06/13/02 13:42
Problem with require ("file");kellog05/30/02 18:45
Why OO in PHP?Bastien05/29/02 10:44
Can't call the same method more than onceRobertoL05/10/02 04:09
RE: Advantages of OO ?crimaniak05/07/02 20:32
RE: Object's ArrayPaul Marconi04/28/02 12:19
RE: Destructor on PHPArnaud04/11/02 13:43
Destructor on PHPpoiu04/05/02 17:05
RE: **real** OO ? have a look at ZopeIan Ferger03/18/02 20:19
why mysql is not supporitng foreign key concesatish mishra03/13/02 06:27
Advantages of OOKarim Ahmed02/11/02 13:58
security vulnerabilityBijan Fathi02/10/02 17:36
this example is full of bugsradiator12/27/01 08:33
about Object Oriented PHP articlevictor12/19/01 13:02
RE: error when tried to connecting to web serverdaniel12/14/01 07:10
RE: error when tried to connecting to web serverNans12/13/01 11:20
RE: Undeclared variableiancforsyth12/10/01 18:01
Use function to get & set values ???Kelvin Sun11/23/01 19:10
RE: Object's ArrayLennie10/28/01 06:06
OO vs. procedural?Ron MacNeil09/25/01 19:44
Error in code on page 4Martin Jørgensen08/09/01 08:15
Undeclared variablepa_bryan07/23/01 23:01
Advantages of OO ?Bernd vd Brincken07/11/01 07:10
error when tried to connecting to web serverHarry C. Sihombing07/09/01 18:47
RE: U HAVE A PROBLEM!!!!Torsten Kammeyer06/19/01 01:58
pharse error?Chaloupka05/27/01 02:04
RE: Cool idea, but.....Charles Palmer05/19/01 12:22
Might be betterEdis Tamoshauskas05/18/01 15:05
Object's Arrayana05/09/01 05:47
U HAVE A PROBLEM!!!!Dolce04/25/01 06:31
RE: "Bug" in this example..Rob04/24/01 13:18
RE: OO logic sucksKen Egervari03/19/01 18:54
PHP, MySQL and objectsMike02/26/01 18:29
OO logic sucksDan Melomedman02/23/01 09:17
mysql.classMark Tonino02/01/01 05:44
**real** OO ? have a look at ZopeDidier01/20/01 10:14
RE: Classes into classes.bill01/04/01 20:36
RE: Classes into classes.bill01/04/01 20:24
Classes into classes.Juan12/13/00 15:40
RE: Is this better?Carsten12/04/00 16:08
Cool idea, but.....Dharssi11/30/00 15:12
RE: Is this better?Gyuro Lehel11/30/00 12:37
Great Eye OpenerDinesh11/28/00 04:35
"Bug" in this example..Eljon Greene09/06/00 17:13
RE: Perl to PHP ObjectsAdrian Kubala08/15/00 17:31
RE: I'm confusedIlja Heitlager08/14/00 18:44
I'm confusedJoe Fan08/14/00 04:43
Object HTMLMichael Ogmios08/09/00 23:17
RE: Prob. with database variablesOmar Rosado08/08/00 17:04
Great, thanksFlorian Oefinger08/06/00 13:55
general commentVincent Vollers08/05/00 16:42
RE: return referencesVincent Vollers08/05/00 16:36
RE: return referencesChris Moylan08/03/00 03:48
RE: Is this better? (short answer: yes)Jeffery Cann08/02/00 18:07
RE: Is this better?Dean Williams08/01/00 23:45
RE: "Printable" version, well....isn't pAndy Rush08/01/00 15:14
RE: Is this better?Frederic De Leersnijder08/01/00 03:57
RE: Is this better?Chuck Gebhard07/31/00 16:48
PHP OO and performanceJens-Petter Salvesen07/31/00 11:02
return referencesJason Miller07/31/00 10:34
Global vars??Juan Borrás07/31/00 03:05
RE: Bad practice in this exampleKyle Olsen07/28/00 17:39
RE: Is this better?Tim Perdue, PHPBuilder.com07/28/00 02:21
RE: "Printable" version, well....isn't pTim Perdue, PHPBuilder.com07/28/00 02:17
Problem with database variableskory gray07/28/00 00:54
RE: Bad practice in this exampletom07/27/00 17:31
"Printable" version, well....isn't printableBrian Sugrue07/27/00 17:02
RE: Bad practice in this exampleTom Anderson07/27/00 15:55
Is this better?Neil Moomey07/27/00 14:18
Bad practice in this exampleKyle Olsen07/27/00 14:03
RE: Perl to PHP ObjectsAdam Constabaris07/27/00 10:24
Perl to PHP ObjectsFred Hirsch07/27/00 09:17
 

If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.