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 10-05-2008, 08:00 PM   #1 (permalink)
The Acquainted
 
Join Date: Sep 2007
Location: Arizona
Posts: 114
Thanks: 10
Andrew is on a distinguished road
Default Tying classes together

Well, I'm reading my OOP book right now, but it doesn't seem to help me TOO much when it comes to tying classes together.

Basically, say I have a user class, which requires access to a database. What would be the best method to access the DB class within the user class? I would guess to have the user class extend the database class, but it doesn't make perfect sense, and also what if I needed another class within it, such as a Validation class?

Is it really good practice to include other class files into one class, and then create an instance within the class itself? Wouldn't that fall victim to including files which in turn also include files, which I'm assuming isn't good practice?

Any insight would be great. This is really driving me nuts, as it's the only thing that's keeping me from starting my project (I don't want to start coding it, and then realize the entire structure is 'wrong').
Send a message via AIM to Andrew Send a message via MSN to Andrew
Andrew is offline  
Reply With Quote
Old 10-05-2008, 08:05 PM   #2 (permalink)
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 335
Thanks: 2
Enfernikus is on a distinguished road
Default

Let's say the name of your DB class is MySQLAccess

PHP Code:

<?php

class user
{
    private 
$db;
        
        public function 
__construct(MySQLAccess $db)
        {
            
$this->db $db;
        }
}

$db = new MySQLAccess($dbInfo);
$user = new user($db);

?>
This way the user class will NEED the object MySQLAccess to start up or it'll give off a fatal error.
Enfernikus is offline  
Reply With Quote
Old 10-06-2008, 06:59 AM   #3 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

You can do that by using the composite design pattern, as Enfernikus showed you above, or you can choose the registry design pattern.
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Old 10-31-2008, 03:16 PM   #4 (permalink)
The Wanderer
 
Join Date: Oct 2008
Posts: 9
Thanks: 0
kjarli is on a distinguished road
Default

Usually it's the first. You store the database object within your user object.

$user->setDatabaseAdapter(new My_Db_Adapter());
kjarli 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 02:04 PM.

 
     

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