PHP classes - scenario help
- From: <news.freedom2surf.net>
- Date: Sun, 12 Apr 2009 23:49:17 +0100
Hi,
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 inserted.
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.
Thanks.
.
- Follow-Ups:
- Re: PHP classes - scenario help
- From: sheldonlg
- Re: PHP classes - scenario help
- From: Geoff Berrow
- Re: PHP classes - scenario help
- From: Jerry Stuckle
- Re: PHP classes - scenario help
- Prev by Date: convert JSP xslt transform to PHP
- Next by Date: Re: PHP classes - scenario help
- Previous by thread: convert JSP xslt transform to PHP
- Next by thread: Re: PHP classes - scenario help
- Index(es):
Relevant Pages
|