|
Object Oriented PHP
Mark Williams
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](mark20000727_drive.gif)
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:
- 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.
- 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.
- 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 example | Ben | 11/07/05 08:38 |
| RE: Problem with database variables | Thomas Noller | 01/11/05 15:36 |
| RE: Bad practice in this example | Thomas Noller | 01/11/05 15:29 |
| Freedom of Choice | nick | 01/06/05 19:14 |
| Fits the real world? A vague claim | Tablizer | 12/29/04 23:13 |
| RE: Why stop at procedural programming? | edis tamoshauskas | 11/26/04 10:13 |
| RE: OO logic is king (but PHP OO sucks) | Steve L | 08/18/04 09:52 |
| RE: "Printable" version, well....isn't printa | Shai | 06/29/04 17:40 |
| RE: Why stop at procedural programming? | Tablizer | 06/15/04 03:17 |
| Why stop at procedural programming? | Pootle | 05/11/04 11:07 |
| RE: Is this better? | acerola | 04/18/04 00:31 |
| Hierarchies Limited (RE: OO vs. procedural?) | Topmind | 04/03/04 14:34 |
| using 'AND' queries with mysqldb class | sandy4door | 12/18/03 14:13 |
| security aspect - constants.inc | Alex Haid | 11/26/03 09:09 |
| RE: No need for Get* and Set* | mmj | 07/02/03 10:20 |
| your script sucks | MindLock | 04/09/03 02:51 |
| even more mistakes | gee | 01/24/03 05:06 |
| This ... | gee | 01/24/03 04:45 |
| OOP Criticism Website | Tablizer | 01/10/03 17:18 |
| RE: OO logic is king (but PHP OO sucks) | Captain Proton | 12/19/02 12:53 |
| RE: Connection | Davide | 12/19/02 06:16 |
| using classes to make your content portable. | Armand Krijgsman | 11/25/02 05:28 |
| RE: OO logic is king (but PHP OO sucks) | Diego | 11/19/02 22:32 |
| RE: OO logic is king (but PHP OO sucks) | Simon622 | 10/25/02 17:11 |
| RE: OO logic is king (but PHP OO sucks) | Diego | 09/29/02 14:18 |
| Connection | Aldrey | 08/22/02 07:30 |
| Could there be "some" mistakes? | C-Man | 08/22/02 03:58 |
| RE: Perl to PHP Objects | Anas Mughal | 08/13/02 14:02 |
| RE: OO vs. procedural? | Anas Mughal | 08/13/02 13:46 |
| RE: return references | Martin | 08/13/02 04:42 |
| RE: OO logic is king | Remmelt | 08/01/02 10:59 |
| RE: OO logic sucks | Tom Myer | 07/30/02 01:45 |
| RE: OO logic sucks | Mike Miller | 07/25/02 00:23 |
| Global Array??? | Abhishek Lahare | 07/12/02 11:11 |
| RE: Object's Array | Eduardo Ramírez | 06/18/02 01:23 |
| No need for Get* and Set* | Stuart | 06/15/02 15:50 |
| RE: OO logic sucks | Tablizer | 06/13/02 13:42 |
| Problem with require ("file"); | kellog | 05/30/02 18:45 |
| Why OO in PHP? | Bastien | 05/29/02 10:44 |
| Can't call the same method more than once | RobertoL | 05/10/02 04:09 |
| RE: Advantages of OO ? | crimaniak | 05/07/02 20:32 |
| RE: Object's Array | Paul Marconi | 04/28/02 12:19 |
| RE: Destructor on PHP | Arnaud | 04/11/02 13:43 |
| Destructor on PHP | poiu | 04/05/02 17:05 |
| RE: **real** OO ? have a look at Zope | Ian Ferger | 03/18/02 20:19 |
| why mysql is not supporitng foreign key conce | satish mishra | 03/13/02 06:27 |
| Advantages of OO | Karim Ahmed | 02/11/02 13:58 |
| security vulnerability | Bijan Fathi | 02/10/02 17:36 |
| this example is full of bugs | radiator | 12/27/01 08:33 |
| about Object Oriented PHP article | victor | 12/19/01 13:02 |
| RE: error when tried to connecting to web server | daniel | 12/14/01 07:10 |
| RE: error when tried to connecting to web server | Nans | 12/13/01 11:20 |
| RE: Undeclared variable | iancforsyth | 12/10/01 18:01 |
| Use function to get & set values ??? | Kelvin Sun | 11/23/01 19:10 |
| RE: Object's Array | Lennie | 10/28/01 06:06 |
| OO vs. procedural? | Ron MacNeil | 09/25/01 19:44 |
| Error in code on page 4 | Martin Jørgensen | 08/09/01 08:15 |
| Undeclared variable | pa_bryan | 07/23/01 23:01 |
| Advantages of OO ? | Bernd vd Brincken | 07/11/01 07:10 |
| error when tried to connecting to web server | Harry C. Sihombing | 07/09/01 18:47 |
| RE: U HAVE A PROBLEM!!!! | Torsten Kammeyer | 06/19/01 01:58 |
| pharse error? | Chaloupka | 05/27/01 02:04 |
| RE: Cool idea, but..... | Charles Palmer | 05/19/01 12:22 |
| Might be better | Edis Tamoshauskas | 05/18/01 15:05 |
| Object's Array | ana | 05/09/01 05:47 |
| U HAVE A PROBLEM!!!! | Dolce | 04/25/01 06:31 |
| RE: "Bug" in this example.. | Rob | 04/24/01 13:18 |
| RE: OO logic sucks | Ken Egervari | 03/19/01 18:54 |
| PHP, MySQL and objects | Mike | 02/26/01 18:29 |
| OO logic sucks | Dan Melomedman | 02/23/01 09:17 |
| mysql.class | Mark Tonino | 02/01/01 05:44 |
| **real** OO ? have a look at Zope | Didier | 01/20/01 10:14 |
| RE: Classes into classes. | bill | 01/04/01 20:36 |
| RE: Classes into classes. | bill | 01/04/01 20:24 |
| Classes into classes. | Juan | 12/13/00 15:40 |
| RE: Is this better? | Carsten | 12/04/00 16:08 |
| Cool idea, but..... | Dharssi | 11/30/00 15:12 |
| RE: Is this better? | Gyuro Lehel | 11/30/00 12:37 |
| Great Eye Opener | Dinesh | 11/28/00 04:35 |
| "Bug" in this example.. | Eljon Greene | 09/06/00 17:13 |
| RE: Perl to PHP Objects | Adrian Kubala | 08/15/00 17:31 |
| RE: I'm confused | Ilja Heitlager | 08/14/00 18:44 |
| I'm confused | Joe Fan | 08/14/00 04:43 |
| Object HTML | Michael Ogmios | 08/09/00 23:17 |
| RE: Prob. with database variables | Omar Rosado | 08/08/00 17:04 |
| Great, thanks | Florian Oefinger | 08/06/00 13:55 |
| general comment | Vincent Vollers | 08/05/00 16:42 |
| RE: return references | Vincent Vollers | 08/05/00 16:36 |
| RE: return references | Chris Moylan | 08/03/00 03:48 |
| RE: Is this better? (short answer: yes) | Jeffery Cann | 08/02/00 18:07 |
| RE: Is this better? | Dean Williams | 08/01/00 23:45 |
| RE: "Printable" version, well....isn't p | Andy Rush | 08/01/00 15:14 |
| RE: Is this better? | Frederic De Leersnijder | 08/01/00 03:57 |
| RE: Is this better? | Chuck Gebhard | 07/31/00 16:48 |
| PHP OO and performance | Jens-Petter Salvesen | 07/31/00 11:02 |
| return references | Jason Miller | 07/31/00 10:34 |
| Global vars?? | Juan Borrás | 07/31/00 03:05 |
| RE: Bad practice in this example | Kyle Olsen | 07/28/00 17:39 |
| RE: Is this better? | Tim Perdue, PHPBuilder.com | 07/28/00 02:21 |
| RE: "Printable" version, well....isn't p | Tim Perdue, PHPBuilder.com | 07/28/00 02:17 |
| Problem with database variables | kory gray | 07/28/00 00:54 |
| RE: Bad practice in this example | tom | 07/27/00 17:31 |
| "Printable" version, well....isn't printable | Brian Sugrue | 07/27/00 17:02 |
| RE: Bad practice in this example | Tom Anderson | 07/27/00 15:55 |
| Is this better? | Neil Moomey | 07/27/00 14:18 |
| Bad practice in this example | Kyle Olsen | 07/27/00 14:03 |
| RE: Perl to PHP Objects | Adam Constabaris | 07/27/00 10:24 |
| Perl to PHP Objects | Fred Hirsch | 07/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. | ||


