Returning a reference to an object

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
choppsta
Forum Contributor
Posts: 114
Joined: Thu Jul 03, 2003 11:11 am

Returning a reference to an object

Post 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!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Why do you have seperate classes, generally you should have one to handle all the database related things.
choppsta
Forum Contributor
Posts: 114
Joined: Thu Jul 03, 2003 11:11 am

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your initial class had design flaws if it required multiple instances for multiple results active.
choppsta
Forum Contributor
Posts: 114
Joined: Thu Jul 03, 2003 11:11 am

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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,
choppsta
Forum Contributor
Posts: 114
Joined: Thu Jul 03, 2003 11:11 am

Post 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?!?!?!
Post Reply