View Single Post
Old 09-14-2012, 04:40 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,449 Times in 2,418 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Easiest way I can think of it is:
If you explicitly rely on a connection of a specific type, pass that into the object:
PHP Code:
$con Connection::getInstance();
$user = new User($con); 
If each class can explicitly make its own connection, its aggregated by the class:
PHP Code:
class User
{
    private 
$con;
    public function 
__construct()
    {
        
$this->con Connection::getInstance();
    }

This is more useful if Connection can be established with multiple connections. In the example of a singleton, it doesn't matter if its passed in or if its constructed within as you will always end up with the same connection. Aggregate it within the class if you want to have the ability to construct an alternate connection. I'd pass it into the class if it must rely on a specific connection (which may or may not be reused by other classes).

So if your individual class is capable of providing alternate storage instructions, I'd allow it to do so by creating it within. If it cannot provide alternate instructions, then I'd pass it as a parameter.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
Oatley (09-14-2012)