Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing database connection by reference in PHP

The question is if a database connection should be passed in by reference or by value?

For me I'm specifically questioning a PHP to MySQL connection, but I think it applies to all databases.

I have heard that in PHP when you pass a variable to a function or object, that it is copied in memory and therefore uses twice as much memory immediately. I have also heard that it's only copied once changes have been made to the value (such as a key being added/removed from an array).

In a database connection, I would think it's being changed within the function as the query could change things like the last insert id or num rows. (I guess this is another question: are things like num rows and insert id stored within the connection or an actual call is made back to the database?)

So, does it matter memory or speed wise if the connection is passed by reference or value? Does it make a difference PHP 4 vs 5?

// $connection is resource
function DoSomething1(&$connection) { ... }
function DoSomething2($connection) { ... }
like image 368
Darryl Hein Avatar asked Oct 27 '08 20:10

Darryl Hein


2 Answers

A PHP resource is a special type that already is a reference in itself. Passing it by value or explicitly by reference won't make a difference (ie, it's still a reference). You can check this for yourself under PHP4:

function get_connection() {
  $test = mysql_connect('localhost', 'user', 'password');
  mysql_select_db('db');
  return $test;
}

$conn1 = get_connection();
$conn2 = get_connection(); // "copied" resource under PHP4

$query = "INSERT INTO test_table (id, field) VALUES ('', 'test')";
mysql_query($query, $conn1);
print mysql_insert_id($conn1)."<br />"; // prints 1

mysql_query($query, $conn2);
print mysql_insert_id($conn2)."<br />"; // prints 2

print mysql_insert_id($conn1); // prints 2, would print 1 if this was not a reference
like image 78
Owen Avatar answered Oct 02 '22 16:10

Owen


Call-time pass-by-reference is being depreciated,so I wouldn't use the method first described. Also, generally speaking, resources are passed by reference in PHP 5 by default. So having any references should not be required, and you should never open up more than one database connection unless you really need it.

Personally, I use a singleton-factory class for my database connections, and whenever I need a database reference I just call Factory::database(), that way I don't have to worry about multiple connections or passing/receiving references.

<?php
Class Factory
{
  private static $local_db;

/**
* Open new local database connection
*
* @return MySql
*/
public static function localDatabase() {
    if (!is_a(self::$local_db, "MySql")) {
        self::$local_db = new MySql(false);
        self::$local_db->connect(DB_HOST, DB_USER, DB_PASS, DB_DATABASE);
        self::$local_db->debugging = DEBUG;
    }
    return self::$local_db;
}
}
?>
like image 45
TJ L Avatar answered Oct 02 '22 16:10

TJ L