objnoob Posted October 25, 2010 Share Posted October 25, 2010 Can I not pass references into class properities from outside the object? class db_manipulation { private $connection = NULL; function __construct(&$database_connection) { $this->connection = $database_connection; if (!$this->connection) die('couldnt connection to database'); } function __destruct() { mysqli_close($this->connection); } } Results in: Warning: mysqli_close() [function.mysqli-close]: Couldn't fetch mysqli in Quote Link to comment https://forums.phpfreaks.com/topic/216797-database-resource-reference-to-class-__construct-method/ Share on other sites More sharing options...
PFMaBiSmAd Posted October 25, 2010 Share Posted October 25, 2010 A) Your code works for me, B) You don't need to use a reference in this case, C) Are you sure your mysqli connection worked? Your code just tests if you have a value, not if it is a valid connection. D) Showing your complete code that produces (reproduces) the error is always the fastest way of getting a solution. Quote Link to comment https://forums.phpfreaks.com/topic/216797-database-resource-reference-to-class-__construct-method/#findComment-1126288 Share on other sites More sharing options...
objnoob Posted October 25, 2010 Author Share Posted October 25, 2010 A) I'm using PHP v 5.1.6. B) I've tried without a reference as well, and all other database interaction methods of this class using mysqli_query, etc. execute fine. C) I can use other methods that query, insert, etc just fine, so the connection is active. D) The code is massive. The only other piece of code used to create the error is the instantiation of the object which is $adminDB = new db_manipulation($mysqli_con); I just can't seem to get the connection to close on __destruct without the warning. I've tried getting PHP upgraded, but we're using RHEL5, and our Linux guy doesn't want to install anything not released through RPM which is somewhat understandable. Thanks for the responses. Quote Link to comment https://forums.phpfreaks.com/topic/216797-database-resource-reference-to-class-__construct-method/#findComment-1126298 Share on other sites More sharing options...
rwwd Posted October 25, 2010 Share Posted October 25, 2010 $adminDB = new db_manipulation(ConnClass $mysqli_con); I recently had something similar to this, and if your connection handle is generated from a class, and your trying to pass a class method into another class, you need to reference the Db class name into the newly instantiated class - as I show in the snippet there. but I agree with @PFMaBiSmAd, there doesn't seem to be anything wrong with the class as you supplied, but there is no need to pass by reference, unless you are trying to save a bit of overhead?? (apologies if I read this wrong - long day at the office) I'm not sure why your doing a _close in your __destruct either as the 'natural' action is for the connection to be closed at the end of the scripts execution - I quote from the manual. Rw Quote Link to comment https://forums.phpfreaks.com/topic/216797-database-resource-reference-to-class-__construct-method/#findComment-1126302 Share on other sites More sharing options...
PFMaBiSmAd Posted October 25, 2010 Share Posted October 25, 2010 For the specific error message you posted, the only way I could produce the same is if $this->connection was a valid mysqli connection, but had already been closed by the time your __destruct() function was called. Without further details of your actual code, it's not possible to directly help further. Edit: I'm going to take a wild guess that you are also passing the mysqli connection into ANOTHER class(es) and one of them is closing the connection when your script ends as well, so you get the posted error in your db_manipulation class? If so, you should not be manipulating the mysqli connection in a class that is just using that connection. You should only manipulate/close the connection in the scope that opened the connection. Quote Link to comment https://forums.phpfreaks.com/topic/216797-database-resource-reference-to-class-__construct-method/#findComment-1126306 Share on other sites More sharing options...
objnoob Posted October 25, 2010 Author Share Posted October 25, 2010 Yeah, I think PHP is closing the resource before my class finalizes aka __destruct. I'll discard my __destruct method and use mysqli_close on my resource in the same scope it was created. Thanks again PF and Rw. Quote Link to comment https://forums.phpfreaks.com/topic/216797-database-resource-reference-to-class-__construct-method/#findComment-1126338 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.