For Programmers: Free Programming Magazines  


Home > Archive > PHP Programming > September 2006 > Is it worth it to return a resource by reference?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Is it worth it to return a resource by reference?
Ney André de Mello Zunino

2006-09-26, 9:57 pm

Hi.

I would like to know whether returning and passing around a resource
(e.g. like those returned by mysql_query) by reference is worth it or
nothing more than overkill. The reason I ask is because I don't know of
what a resource is actually made and I fear it might be so simple (e.g.
a mere number or pointer identifying a potentially big object) that
passing it by reference could even increase the overhead. What's the truth?

The situation I have is a function which queries and returns a given
range of objects (blog posts in this case) and another one which later
receives the result set and works through it to output the contents.
Something like the following sample code (using references):

// ----- BEGINNING OF SAMPLE CODE -----

function& getPosts(...)
{
return mysql_query(...);
}

function showPosts(&$postsResultSet)
{
while ($post = mysql_fetch_object($postsResultSet))
{
...
}
}

// driver code:
$results =& getPosts(...);
....
showPosts($results);

// ----- END OF SAMPLE CODE -----

Any comments will be appreciated.

Thank you,

--
Ney André de Mello Zunino
Jerry Stuckle

2006-09-27, 6:59 pm

Ney André de Mello Zunino wrote:
> Hi.
>
> I would like to know whether returning and passing around a resource
> (e.g. like those returned by mysql_query) by reference is worth it or
> nothing more than overkill. The reason I ask is because I don't know of
> what a resource is actually made and I fear it might be so simple (e.g.
> a mere number or pointer identifying a potentially big object) that
> passing it by reference could even increase the overhead. What's the truth?
>
> The situation I have is a function which queries and returns a given
> range of objects (blog posts in this case) and another one which later
> receives the result set and works through it to output the contents.
> Something like the following sample code (using references):
>
> // ----- BEGINNING OF SAMPLE CODE -----
>
> function& getPosts(...)
> {
> return mysql_query(...);
> }
>
> function showPosts(&$postsResultSet)
> {
> while ($post = mysql_fetch_object($postsResultSet))
> {
> ...
> }
> }
>
> // driver code:
> $results =& getPosts(...);
> ...
> showPosts($results);
>
> // ----- END OF SAMPLE CODE -----
>
> Any comments will be appreciated.
>
> Thank you,
>


Passing by reference should never increase the amount of overhead (at
least not by any measurable amount), even for an integer. And for more
complex objects it will decrease the overhead.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Ney André de Mello Zunino

2006-09-27, 6:59 pm

Jerry Stuckle wrote:

> Passing by reference should never increase the amount of overhead (at
> least not by any measurable amount), even for an integer. And for more
> complex objects it will decrease the overhead.


Thanks for your reply. Given that the use of references can never hurt
and that this is my first time using them, could you tell me if I am
doing it right (according to the sample code in my original post)?

Thank you,

--
Ney André de Mello Zunino
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com