PHP classes - scenario help

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I have a question about how to approach a programming task when using
classes in PHP. I'll keep my explanation brief as possible hoping you can
make typical assumptions.

E.g. I have a class called "Suppliers". This is related to the management of
suppliers in my database. When inserting a new supplier there is a HTML page
which has all the fields associated with a supplier. One of these fields is
email address.

The email address value is not actually held in the supplier DB table.
Instead it is held in an email DB table. The email table also holds
information related to what record a particular email address is related to.
This is because the email address table is used to hold not just email
addresses for suppliers but also email addresses for other entities such as
clients, etc. (part of my normalization procedure I believe).

So when the user presses submit on the "insert supplier page", all of the
form values that have been entered are passed to another form which will
create the supplier class and call the insert function of it.

This is when I started to get a bit confused about the use of classes.

My initial plan was to pass all the form variables to the "insert" function
of the "suppliers" class. But then I realised, actually I have an "email"
class that is supposed to be used for the management of emails.

So when in the "Supplier" I came to the processing of the email field, I
suddenly realised that I need to create another class from within my
supplier class (an instance of the email class). Therefore calling a class
from another class. Earlier research showed this was not really possible. So
this left me thinking, how do I do this?

Further thinking has led me to conclude that I actually have to create all
the different classes one at a time from the page processing the form input.
Therefore at first I would create my "supplier" class, and call the insert
value to insert the fields directly relevant to the supplier class.

Then, having stored the new ID of my supplier, I then create an email class
and pass the relevant values to the insert function of the email class.

This seems to me the only way I can do it (and also a new way of me thinking
about this all).

My problem a little with this method is that how do I handle things such as
roll back. Lets say there was an error inserting the email field that meant
I did not want to the supplier record to be inserted either. This way I
would have to manually roll back and delete the supplier record I had just

Or even more difficult. Say I had created the tables so that a supplier
cannot be inserted without there being a related email ( I guess I just have
to insert the email first).

Anyway, I hope I have made the source of my confusion clear and hopefully
someone can confirm they ways I have thought to solve these problem are the
correct ways and I am not missing something about classes that makes things
easier to work with.

Thanks in advance even if you have managed to read and understand what I'm
on about.


Re: PHP classes - scenario help wrote:
Quoted text here. Click to load it

First of all, it depends on a lot of things.  This might be properly
normalized, or it might not.  Database normalization does follow rules,
but there is some leeway in the rules.

You need to follow up in a MySQL newsgroup for more information on the
database end.

As for your C++ design, there are a couple of ways to do this.  You can
have two "data" objects, one for suppliers and one for emails.  When you
go to insert the row, you will need to create both objects, fill in the
appropriate values and insert the data.

Alternatively, you can create a "business object" which deals with both
tables.  It takes all of the data from the form and inserts the proper
data into the proper tables.  In a more complicated scenario, this might
even create the "data" objects, but is not required to do so.

Either is perfectly valid, and which is better is dependent on what else
you're doing in the program.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: PHP classes - scenario help

<> contained the following:

Quoted text here. Click to load it

Well I'm not the world's greatest database expert, but that doesn't
sound right to me...
Geoff Berrow  0110001001101100010000000110
100110001101101111001011100111010101101011 -

Re: PHP classes - scenario help

Thanks for the replies. All useful stuff to think about.

Re: PHP classes - scenario help wrote:
Quoted text here. Click to load it

Look at for transactions in php.

Site Timeline