diving into php 5... OOP design questions

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
bg
Forum Contributor
Posts: 157
Joined: Fri Sep 12, 2003 11:01 am

diving into php 5... OOP design questions

Post by bg »

Well, I decided to finally download php5 and get down and dirty with it. I was waiting for it to come out before I started my first big *real* OOP project, for obvious reasons.

I want to develop a fitness log program, with the following functionality:

- Track workouts: date, time, sets, reps, weight and exercise
- Track cardio sessions: date, time, duration and intensity
- Track diet: date, time, meals, calories, carbs, protein, fat
- Track Supplements: date, time, supplement name, dosage, method of administration
- Track measurements: date, body part, size (length)
- Track weight: date, weight

- Display graphs and other visual representations of progress using tracked information (tentatively using JpGraph, who should be releasing a php 5 version soon)

Now I was wondering the best way to go about doing this from an OOP perspective. My initial thought would be to create objects for each of the tracked informations, so there would be a class for each of the following:

workout
cardioSession
meal
supplement
measurement
weight

In addition, there would be an exercise class, which there would be a collection of in the workout class (because a workout consists of multiple exercises).

Some things ive thought about:

A weight class might be redundant. It is a measurement, but not a length measurement, so maybe it could part of a more functional measurement class.

Another concern I have is that I may be going at this completely wrong...

Any help and suggestions would be appreciated.
penguinboy
Forum Contributor
Posts: 171
Joined: Thu Nov 07, 2002 11:25 am

Post by penguinboy »

Hmm...

IMO you wouldn't need all those classes.

If you were developing a game .. or some simulation I could see using them.

Really, you're just going to be:
*posting data via webforms
*?storing it in a db
*?retrieving it from a db
*analyzing data for graphs
*displaying it on the page
bg
Forum Contributor
Posts: 157
Joined: Fri Sep 12, 2003 11:01 am

Post by bg »

I see what your saying but I think you are over-simplifying the task at hand. My reasoning behind using a class for each of those is that it will organize all of the data and make it easily accessible for whatever graph someone might want to view. I dont want this software to give the user a set of predefined graphs for them to view, I want them to choose what is graphed with the data they want. I figure it would be much easier to implement this using multiple classes.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

If you're just starting OOP (which incidentally works very well indeed in php4) be prepared for a lengthy learning process. You're first object designs aren't going to be good. You'll go through several rewrites - and lots of Eureka! moments - before arriving at a solid, final solution. The design section at http://www.phppatterns.com provides much food for thought.

Eclipse is well worth taking some time to look at. It's a good example of tightly-focussed classes which do just one thing.

There are also a few OOP links on the new devnet Wiki.

A lot of this stuff takes time to sink in. OOP is really pretty simple - but "simple and profound".

I'm not sure if I know enough about fitness regimes to say how you should cut the OOP cake here (although I do a bit of mountaineering and cycling I'm also a nicotine addict so I'm maybe not the best person to ask).

Anyway, I guess the aim here is to show how various factors are impacting on fitness. An object for each graph sounds about right - but not necessarily a separate class for each graph. You can probably create a generic Graph object which displays particular graphs. You would have different classes to gather data for each type of graph, each providing the same interface for the Graph object to draw the actual graph.

As I say, I don't really know enough about the problem to give the best advice - and I havent used JpGraph which for all I know already provides a generic Graph class.
Last edited by McGruff on Fri Jun 04, 2004 6:13 pm, edited 1 time in total.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

penguinboy wrote:Hmm...

IMO you wouldn't need all those classes....
You're right: you don't need objects for any of this. It's just that, with OOP, you can do it so much better.

OOP doesn't allow you to do anything you can't do with procedural code. It's not about telling a computer to do something: it's all about good writing practices - literary programming rather than spaghetti programming.

The benefits are all at the programmer's end: the computer doesn't care what kind of code you write but, for the programmer, OOP is much more modular and so easier to work with and maintain. That goes double if you are working in a team.

OOP also promotes code re-use. For example, defining collections as iterators allows you to use the same Table decorator "blindly" to print arrays, query results, etc - anything at all which can be defined as an iterator.

I've written my share of spaghetti in the past - some of it still serving up web pages on live sites. I have nightmares about getting a call from a client with a problem to fix. I haven't got a clue how it all works - and I wrote it. If everything is jumbled up together it's incredibly difficult to track down problems or add new features.

It's not quite true to say that anything non-OOP is spaghetti but the aim of disentangling all the various threads in a script, isolating discrete tasks in separate units, inevitably leads you towards OOP designs.

As for the number of objects, you might easily use 30-40 classes to display a forum posts list such as this page. That's good: it shows you've got nice, tightly-focussed class definitions (see Eclipse link, above).
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Can anyone provide me a link, or explanation, on why classes help, vs a bunch of functions?

Whats wrong with having a common.inc file with all the common functions, then a seperate .inc file for each additional one?
fastfingertips
Forum Contributor
Posts: 242
Joined: Sun Dec 28, 2003 1:40 am
Contact:

Post by fastfingertips »

Hmm i will try to give you an example, in this way i hope that you will understand the difference.

You must create som e functions that are calculating the area of different geometric forms like squares, rectangles, triangles etc.
Ordinary you will create 3 functions that will calculate area for each type and were you will need the area you will past the code or call the function, ok? But if your boss will ask you to calculate also the perimeter of this types you will have to do following steps:
- create new functions or up-grade the current one
- remember everywere were this functions are written and change the body.
As you may notice this will eat a lot of your time.

Now the easy way, the OOP is to create a generic class named geometrics
that allow you to define following methods:
area and also some properties like: number of (doh my english) lines (if is triangle or square) and an aria (will be a number) property.

From this class you will extend you child classes: square class, triangle class etc with different implementation for area. Now if you will need to calculate the perimiter you will add in the generic class the perimeter method and also a perimeter property and all classes derivated from it will know what perimeter is :), and what you will have to do is just to implement the perimeter for each one.
Post Reply