Jump to content

pass mysql connection to an object as reference or doesnt matter?


arianhojat

Recommended Posts

In chapter 9 of Object-Oriented PHP: Concepts, Techniques and Code,

it makes an MySQLConnect class which uses a MySQLResultSet class to create a resultset.

i was wondering if the $this->connection in the createResultSet() function should be passed as a reference to the mysql connection, currently it is sending a copy of that resource which is wasteful, right?

or is it automatically passed as reference since it is an object property.

 

class MySQLConnect {

private $connection;

...

function createResultSet($strSQL, $databasename){

    $rs = new MySQLResultSet($strSQL, $databasename, $this->connection );

    return $rs;

}

Link to comment
Share on other sites

Daniel0, well in constructor for the MySQLResultSet there is no & in the constructor func definition,

so pass by reference i dont think exists in that case right?

class MySQLResultSet{

private $connection;

...

public function __construct( $strSQL, $databasename, $connection ){ //not &$connection

 

 

 

 

Jenk, Is the property/resource of the object that is passed in viewed as an object?

i can understand if the MySQLConnect object itself is passed in by reference since all Objects passed by reference as you said, but what about properties of objects?

Link to comment
Share on other sites

So is a mysql resource connection viewed as an object then?

 

it was created via mysql_connect();

 

i know mysqlI(mproved) in php manual says it returns an object. but regular mysql_connect which is what they use returns a

'MySQL resource link identifier'. Can this be viewed as an object then?

Link to comment
Share on other sites

cool, didnt see resources are Passed by Reference in php manual i have downloaded.

 

I just suppose they are. You could test it with this:

 

<?php
function test($var)
{
$var = 'hello';
}

$connection = mysql_connect('localhost', 'root', '');
test($connection);

echo ($connection == 'hello' ? 'not ' : null).'passed by reference';
?>

 

Even if it isn't passed by reference, it doesn't matter. It'll still work.

Link to comment
Share on other sites

Daniel0, if ($connection == 'hello'), then its passed by reference, right since value was changed by function? (i think your example had opposite)

 

Anyway tested it out...

<?
function test($var)
{
$var = 'hello';
}

$connection = mysql_connect($host, $user, $pass) or die("Unable to connect!"); //also tested with object: $connection = new mysqli($host, $user, $pass);

echo $connection ."<br/>";
test($connection);
echo $connection ."<br/>";

 

and get Resource#1 each time, so the connection isnt passed by reference unless explicit in function definition.

Link to comment
Share on other sites

Technically that's not exposing anything. A better test would be to use the connection as a connection would be used, then compare scopes. The values are passed by reference, not the variables. There is a big difference there.

 

$connection = mysql_connect(/* blah */);
mysql_select_db('db', $connection);

function doSomething ($var) {
  mysql_query('INSERT INTO `table` VALUES(\'a value\')', $var);
}

echo mysql_insert_id($connection);
doSomething($connection);
echo '<br />';
echo mysql_insert_id($connection);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.