Page 1 of 1

class implementation problem

Posted: Thu Feb 17, 2005 8:35 am
by dannymc1983
does anyone know how one class can use the methods and data of another class without using ingeritance??
for instance im creating a database that will store profile information on say employees of a company. there are 2 types of employees, staff and managers. i create one class for staff and one for managers. the managers class extends the staff class using inheritance. each class contains methods to get profile information, modify, update etc.
now i create a third class containing all the database code (sql code for example) for manipulating the databases tables. what id like to know is how exactly do i use this class in my staff and manager classes. For example the view profile method will require a method from the database opertations class to get data from a database table.
anyone have a solution to this??? any help will be much appreciated, thanks!!

Posted: Thu Feb 17, 2005 8:40 am
by feyd
make the database class a singleton, then either pass the singleton object into the classes, or use a singleton factory to retrieve the object inside your user objects. Either store the singleton reference in the user objects, or keep calling the singleton factory.

Posted: Thu Feb 17, 2005 8:45 am
by dannymc1983
thanks for your reply.
sorry but im fairly new to php. could you explain what a singleton is and maybe just give a quick code example of the procdure you just outlined. thanks for the help

Posted: Thu Feb 17, 2005 8:54 am
by Maugrim_The_Reaper

Posted: Thu Feb 17, 2005 9:04 am
by dannymc1983
thanks for that.
i have just one more question. about the design that i have outlined above for the profiles, is this the best way of going about it do you think???
if not would you know of a better way?

Posted: Thu Feb 17, 2005 9:10 am
by feyd
depending on implementation, it's a good way. I'm not sure about best, as there are many approaches to take on any OOP task, of which a few patterns are typically well suited for it.

Posted: Thu Feb 17, 2005 9:12 am
by dannymc1983
do you know of any sites that outline a good design and implementation?

Posted: Thu Feb 17, 2005 9:14 am
by feyd
Nope, I can only recommend books on the subject.. which we've already discussed in previous threads.