TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-21-2008, 01:41 AM   #1 (permalink)
The Visitor
 
Join Date: Feb 2008
Posts: 2
Thanks: 0
widox is on a distinguished road
Default Handling class dependencies

I'm looking to see what other's do in situations where a class is dependant on other classes. For instance if the class User needs to use the class Database.

For instance, do you first create an instance of Database and pass that into a User object via the constructor or create the instance of the Database class *in* User's constructor? Or something completely different.

It's obviously dependant on the project, but I'm just speaking generally on how you've done it in the past and how you came to that decision. Just looking to spur some discussion.

Regards,
Matt
widox is offline  
Reply With Quote
Old 02-21-2008, 02:46 AM   #2 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

I would pass the instance via the constructor rather than create new instances within each object that requires db access.

My preferred solution is to use a registry object to store a single instance of the db, this allows access the db instance from any class.
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Karl is offline  
Reply With Quote
Old 02-21-2008, 12:00 PM   #3 (permalink)
The Contributor
 
flyingbuddha's Avatar
 
Join Date: Jan 2008
Location: Birmingham, UK
Posts: 60
Thanks: 10
flyingbuddha is on a distinguished road
Default

I'm currently reading through Essential Actionscript 3. I think I've used what Colin describes as composition throughout most of my OOP projects without realising the name for it:

moock>> composition vs inheritance, from essential actionscript 2.0 (eas2)
__________________
Pro. Geek
http://www.mikeholloway.co.uk

Last edited by flyingbuddha : 02-21-2008 at 12:01 PM. Reason: Just realised the link is pointing to AS 2, but it's the same theory.
flyingbuddha is offline  
Reply With Quote
Old 02-21-2008, 06:28 PM   #4 (permalink)
The Visitor
 
Join Date: Feb 2008
Posts: 2
Thanks: 0
widox is on a distinguished road
Default

Thanks both for your input. I'll be checking out those resources.

I've done both, and I'd probably agree with Karl. At least if you make a instance of a class and pass it in to another you could at least reuse that object somewhere else.
widox is offline  
Reply With Quote
Old 02-24-2008, 03:21 PM   #5 (permalink)
The Contributor
 
Join Date: Nov 2007
Posts: 32
Thanks: 5
Morishani is on a distinguished road
Default

I would pass the instance too, by reference.
__________________
מטבחים (hebrew)
Send a message via ICQ to Morishani Send a message via MSN to Morishani
Morishani is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 06:56 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design