[SOLVED] Classes and scope

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

[SOLVED] Classes and scope

Post by magicrobotmonkey »

Ok here is the scenario. I have one class, lets call it 'dad' and another class 'junior'. Either 'dad' or 'junior' can be used directly from various scripts. If dad is used, he will create a bunch of 'juniors' (heh).

I also have a class 'db' which is a database abstraction class.

The 'dad' and 'junior' classes will be called from scripts which may or may not be using the db abstraction class so I can't assume that I will be passed this class.

So whats the best way to even do this? Is just passing it to the constructor good? What if I pass it by reference (thats what I think is best). Is there another way to make the 'db' class accessible to everyone? I dont want to automatically build a 'db' class in everyone of these, because it will create a lot of overhead.

is it right to do something like this:


Code: Select all

<?php

class dad 
{
    var $db;

    function dad(&$thisDB)
    {
        if(!is_object($thisDB))
        {
             include(db.class.php)
              $this->db = new db($connectinfo)

        }
        else
              $this->db = $thisDB
    }


}

?>
*sigh* I'm not sure what to do....
Last edited by magicrobotmonkey on Wed Dec 08, 2004 10:07 pm, edited 1 time in total.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Yeah thats fine, i always pass class references to other classes, and do it just like this - using an array of class objects if i have to pass more than one.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

Solved in your own post - :). Passing the object by reference is just fine - I certainly don't use anything else...
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

If you're using php4:

Code: Select all

$foo =& new Foo;

function foo(&$object)
{
    $this->foo =& $object;
}

function &getFoo()
{
    return $this->foo;

    // or:
    return new Foo;
}
Last edited by McGruff on Sun Aug 07, 2005 8:27 am, edited 1 time in total.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

now you've gone over my head

Code: Select all

<?php

//"create by reference??" whats this one do?
$foo =& new Foo;

function foo(&$object)
{
    //isn't the one in the function parameter list enough or is this one needed as well?
    $this->foo =& $object;
}


//what does it mean to pur this on a function?
function &getFoo()
{
    return $this->foo;

    // or:
    return new Foo;
}
?>
Post Reply