tags:

views:

658

answers:

4

I am coding my first object factory, and I am wondering if there any any drawbacks to passing one object into another when creating the second object, and then saving the first object within the second for use later?

Example:

class main
{
    public $connection = array();

    // various other useful functions that should be loaded on every page here
    // these other functions would be convenient to have avail for 
    // new objects in different classes

    function dbconnect($db) {
        if(!isset($this->connection[$db])) {
            switch($db) {
                case "users":
                    $connection = @mysql_connect(info,info,info);
                    $db_select = @mysql_select_db(info,$connection);
                    $this->connection[$db] = $connection;
                break;

                case "ads":
                    $connection = mysql_connect(info,info,info);
                    $db_select = mysql_select_db(info,$connection);
                    $this->connection[$db] = $connection;
                break;

                default:
                    die($db . " is not a recognized database.");
                break;
            }
        }

        return $this->connection[$db];
    }

    // note how i pass $this into the new object in this function
    function create($class,$params=null) { 
        if(!class_exists($class) && !$this->load($class)) {
                die($class . " is not a recognized class.");
        }else{
                return new $class($this,$params);
        }
    }

    function load($file) {
        $file = $_SERVER['DOCUMENT_ROOT'] . "/classes/" . $file . ".class.php";
        if(file_exists($file)) {
                return require_once($file);
        }else{
                return false;
        }
    }
}

class secondary {

    private $connection;
    private $mainObj;
    private $params;

    function __construct($mainObj,$params) {

        $this->mainObj = $mainObj;
        $this->params = $params;

        $this->connection = $mainObj->dbconnect('users');

    }
}

As you see here, any class created by the factory conveniently has the ability to connect to the MySQL db if the class requires it (and other common/useful functions) ... without having to use globals, or pass many things in.

Is this a stable way? I am new, and could use some good theory/schooling on why this is wrong - if it is.

+2  A: 

Yes, it is a pretty stable way. But I would like to see type hinting in the methods taking outer instances as parameters, which would make your classes typed stronger.

Example;

class Alob {
    public function aMethod(**Blob** $outerObject) {
        ...
    }
}

class Blob {
    ...
}

This will throw a fatal error if Alob::aMethod() is called with another type than Blob.

I would also like to see more accessor keywords for defining visibility, hide your class privates with 'private' and so on.

Björn
okay, i haven't walked down that path before...with the type hinting...i guess i dont understand the necessity of it...but i will check that link you left:)
johnnietheblack
just read up on type hinting...i get it - it LOCKS that parameter as being my object that Im passing since thats what the class will need to survive. in the example above, it would HAVE to be an object of class 'main'. thanks!
johnnietheblack
+1  A: 

One this you'll have to be careful with, with that solution of keeping an object accessing the DB (which is not bad ; you should read about the "Dependancy injection" pattern, btw), is to not disconnect from the DB as long as you still need to be able to access it
(Yeah, seems pretty logical ^^ )

What I mean is it might become easy to forget that some object somewhere needs the DB connection not to be closed.

Easy way would be to never disconnect -- PHP will automatically close the connection to the DB server at the end of the script, anyway.

Pascal MARTIN
I will definitely read up on dependancy injection...however, I am missing the risk that you were talking about with the db closing? if i dont have any closing functions anywhere, in any class that the object might be passed to, i shouldn't have a problem, right? or no? thanks for you answers:)
johnnietheblack
If you never disconnect, no, you won't have a problem (and that's why I'd do ^^ ) ; sorry if I didn't make that clear :-(
Pascal MARTIN
+2  A: 

Definitely nothing wrong with object passing. If you have a ClassB object that needs an instantiated object of ClassA in order to function, then your only options are to create that ClassA object inside ClassB, or to pass ClassA in.

If the ClassA object is likely to be used by multiple ClassB objects, like in your example, then passing the ClassA (ie. Database connection object) around is actually more efficient than the alternative, which would involve a lot of duplicate objects being created inside each class.

Since PHP5 automatically passes objects by reference, setting up a reference inside your secondary class to $mainObj is actually very efficient.

zombat
A: 

Not only is this ok, but it's actually one of the cornerstones of OOP, and it's called aggregation

Peter Bailey