class implementation problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dannymc1983
Forum Commoner
Posts: 80
Joined: Wed Feb 16, 2005 7:24 am

class implementation problem

Post 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!!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
dannymc1983
Forum Commoner
Posts: 80
Joined: Wed Feb 16, 2005 7:24 am

Post 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
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

dannymc1983
Forum Commoner
Posts: 80
Joined: Wed Feb 16, 2005 7:24 am

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
dannymc1983
Forum Commoner
Posts: 80
Joined: Wed Feb 16, 2005 7:24 am

Post by dannymc1983 »

do you know of any sites that outline a good design and implementation?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Nope, I can only recommend books on the subject.. which we've already discussed in previous threads.
Post Reply