Page 1 of 1

Returning NULL by reference

Posted: Tue Mar 07, 2006 9:30 am
by llfitness
I'm writing a handler for all my database calls. Because of the possibility that the function might be returning a LOT of data, I want it to be as fast as possible. In college I learned that, as long as the originating function wasn't going to be using a value any more, returning by reference was faster than returning by value.

- Is this true for PHP?
- If so, what is the "proper" way to return NULL, if necessary, by reference?

Although not necessarily relevant to my question, here is the code I am using for the db_handler. Note that I am using an error handler to collect information. Anything

Code: Select all

function &db_call($sql, $return_type)
	{
	// defined return types:
	// none: SQL_RESULT_NONE
	// single: SQL_RESULT_SINGLE
	// set: SQL_RESULT_SET
	// object: SQL_RESULT_OBJECT 
	
	global $db;

	// trigger_error("Looking closely at DB call:\n\n" . $sql . "\n\n" . 
		urldecode($sql), E_USER_NOTICE);

	$result = $db->Execute($sql);

	if ($result === FALSE)
		{
		trigger_error($db->ErrorMsg(), E_USER_ERROR);
		}
	elseif ($return_type == SQL_RESULT_NONE)
		{
		assert($result->RecordCount() == 0);
		return 0;
		}
	elseif ($return_type == SQL_RESULT_SINGLE)
		{
		if ($result->RecordCount() == 1)
			{
			return $result->fields;
			}
		else
			{
			return NULL;
			}
		}
	elseif ($return_type == SQL_RESULT_SET)
		{
		if ($result->RecordCount() > 0)
			{
			return $result->GetRows();
			}
		else
			{
			return NULL;
			}
		}
	elseif ($return_type = SQL_RESULT_OBJECT)
		{
		return $result;
		}
	else
		{
		trigger_error('This is an unexpected condition. 
			Check the second arguement of db_call... 
			or there may be other causes', E_USER_ERROR);
		return NULL; // this code will probably not happen.
		}
	} // END function db_call(...)

Posted: Tue Mar 07, 2006 10:37 am
by feyd
set the variable to null, then return that.