Page 1 of 1
Returning a reference to an object
Posted: Thu Sep 16, 2004 10:01 am
by choppsta
I'm trying to learn OOP and have started by putting together my own database abstraction type class.
I've come up with two classes:
Db_Connection - This handles the connection to the database
Db_Query - This handles the querying and results
The idea is that I have one Db_Connection object which when I use the query method it returns a Db_Query object.
The query method is something like:
Code: Select all
<?php
function &query($query) {
$result =& new Db_Query($this->connection, $query);
return $result;
}
?>
And then the client code looks like:
Code: Select all
<?php
$result =& $db->query("SELECT * FROM Blah");
?>
So my question is, do I have all the "&" in the right place?
I want to make sure that the query object is only created once and not copied when being returned.
According to (my understanding of) the PHP manual this should do what I want it to, but it's very difficult for me to work out if it actually is!!
Any comments would be greatly appreciated!
Posted: Thu Sep 16, 2004 10:24 am
by John Cartwright
Why do you have seperate classes, generally you should have one to handle all the database related things.
Posted: Thu Sep 16, 2004 10:47 am
by choppsta
I did start off with one class, but I realised that in order to have more than one result set at one time it meant instantiating a new object, supplying the database connection parameters again, etc.
Having them seperated means I can have one object that is my connection and this object can then produce as many result sets as I need.
Posted: Thu Sep 16, 2004 11:24 am
by feyd
your initial class had design flaws if it required multiple instances for multiple results active.
Posted: Thu Sep 16, 2004 12:01 pm
by choppsta
Ok, firstly let's ignore whether this is the right way to do this. My question was whether or not I was returning a reference to an object in the way that I had intended.
As for having one class to "handle all the database related things.", this seems to go against other things I've read about not having classes be responsible for too much, and that each class should perform one thing.
Also, I noticed that the way i'm trying to do this is infact very similar to Microsoft's DAO. You have a database object which then returns result set objects.
Simply posting saying that I'm wrong isn't really helping me very much.
Posted: Thu Sep 16, 2004 12:17 pm
by John Cartwright
I never said your method was incorrect...
In my opinion would this would be more effective
class db
function connect
$this->connect = blah
function query
$query = $sql statement, $this->connect
return $query
I just dont see why you'd have to instantanitate 2 different classes for similar functions
by the way... saying having a class not contain too much functions, and have specific classes for specific functions really means not to have 1 class for all your functions, I see no problem with just a few thought,
Posted: Fri Sep 17, 2004 9:23 am
by choppsta
From the code you've provided it appears that your class returns a variable $query.
I presume this is either a mysql result which you would then have to do mysql_fetch_row() or similar on, defeating the point of having a database abstraction. Or, it could be an array of the data. In that case you're pulling all the data out into memory in one go, which I don't want to do.
My method keeps all the database stuff encapsulated.
Now, as I said in my last post, this is not my question!!
Let's just say that i'm trying to produce a factory class that returns different objects depending on the input. In this instance I'd want to return an object by reference.
Is the way I have shown above the correct way?!?!?!
Does anyone here know anything about references?!?!?!