Here’s where I have enough knowledge to be dangerous.
I’m working on a web app which has different types of interrelated classes. For example, there is an “Item” class and a “User” class. The “Item” class has a property called “Owner” which refers to a “User” object. Likewise, the “User” class has a property called “Items_array” which is an array that holds (you guessed it) “Item” objects.
My understanding is that objects were automatically returned as a reference, so I have functions for fetch_item, fetch_user, etc. These keep track of a master array of objects, returning the correct one if it already exists and creating a new one if it does not exist. Below is the code to get a user, for example.
public function get_user ($userID) {
if (!isset ($this->users_arr[$userID]))
$this->users_arr[$userID] = new User($userID);
return $this->users_arr[$userID];
}
I’ve had properties in my Items class storing User objects with no problem. This morning I added some properties to my User class that store Item objects and now I keep getting ‘Unable to open database connection’ errors. I’ve isolated this to the creation of the Item objects – when I comment that out everything works fine. I’m fairly sure I’ve created some kind of infinite loop where rather than using references, Items are creating Users, which are in turn trying to recreate the Items.
Does this make sense? Anyone run into something like this before or have any thoughts on how to resolve the issue?
The only reason I can think of to get “Unable to open database connection” error because of addition of some code is that you open too many of them. MySQL, for example, has a limit of 30 connections per user (that would be you). Normally you shouldn’t have more than one connection per script and if you don’t store your connection handle globally you can have made an error exactly there.