arianhojat Posted December 3, 2007 Share Posted December 3, 2007 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; } Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted December 3, 2007 Share Posted December 3, 2007 If you are using PHP5, and since you use the private keyword you must be, you don't have to do so. In fact the call-time pass-by-reference is deprecated in PHP5. See: http://php.net/language.references.pass Quote Link to comment Share on other sites More sharing options...
Jenk Posted December 3, 2007 Share Posted December 3, 2007 All objects are passed by reference in php5+ Quote Link to comment Share on other sites More sharing options...
arianhojat Posted December 3, 2007 Author Share Posted December 3, 2007 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? Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted December 3, 2007 Share Posted December 3, 2007 Only if it is an object as well. If it's just a regular string or int then it won't be passed as a reference. Quote Link to comment Share on other sites More sharing options...
arianhojat Posted December 3, 2007 Author Share Posted December 3, 2007 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? Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted December 3, 2007 Share Posted December 3, 2007 No, but resources are passed by reference as well. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted December 3, 2007 Share Posted December 3, 2007 @arianhojat You can always easily test if something is passed by reference or not. In the function where it is received, just change its value. If the change is reflected in the original object / variable, then it was passed by reference. Quote Link to comment Share on other sites More sharing options...
arianhojat Posted December 3, 2007 Author Share Posted December 3, 2007 cool, didnt see resources are Passed by Reference in php manual i have downloaded. thanks Daniel0 and roopurt18. Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted December 3, 2007 Share Posted December 3, 2007 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. Quote Link to comment Share on other sites More sharing options...
arianhojat Posted December 4, 2007 Author Share Posted December 4, 2007 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. Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted December 4, 2007 Share Posted December 4, 2007 Yeah, you're correct. My snippet showed the opposite in fact. Quote Link to comment Share on other sites More sharing options...
Jenk Posted December 4, 2007 Share Posted December 4, 2007 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); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.