andy_b42 Posted February 9, 2009 Share Posted February 9, 2009 I am fairly new to using objects in php. I have made a fairly simple database object so i can just call one of a few of my own methods to automatically make changes to the database when i change the relevant object. Below is a very simplified version of what i am trying to accomplish: Class Database{ public function Query($query){ //connect to database, run query and return result } } Class User{ private $username; private $db; public function __construct($intID){ $this->db = new Database(); if($intID > 0){ getUserData($intID); } } public function getUserData($intID){ $sql = "select * from users where intID = $intID"; return $this->db->Query($sql); } } The problem i get is when "$this->db->Query($sql);" is executed and the error i get is: "Fatal error: Call to a member function Query() on a non-object" According to the IDE i am using, the function is accessible, but it seems when running the code, the parser cannot see that $this->db is an object with functions. My question is, is what i am attempting to do possible or am I missing something very basic to make this work? Thanks Quote Link to comment Share on other sites More sharing options...
trq Posted February 9, 2009 Share Posted February 9, 2009 My question is, is what i am attempting to do possible or am I missing something very basic to make this work? Not only is it possible, but the example you have supplied should actually work. Although this.... public function __construct($intID){ $this->db = new Database(); if($intID > 0){ getUserData($intID); } } should be.... public function __construct($intID) { $this->db = new Database(); if ($intID > 0) { $this->getUserData($intID); } } Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted February 9, 2009 Share Posted February 9, 2009 It should work indeed can't find an error. Unless you do something like this to call the function User::getUserData(0); instead of $user=new User(); $user->getUserData(0); what does the code look like to instantiate the user? Quote Link to comment Share on other sites More sharing options...
andy_b42 Posted February 9, 2009 Author Share Posted February 9, 2009 Thanks for the quick replies The objects i am actually using are a lot more complex than what i have posted here: below are the database and user objects http://andyb42.servebeer.com:4567/phpfreaks/database.txt http://andyb42.servebeer.com:4567/phpfreaks/user.txt The user is instantiated like this "$user = new User($intID);", the constructor then does all of the work filling in the users data from the database. Quote Link to comment Share on other sites More sharing options...
trq Posted February 9, 2009 Share Posted February 9, 2009 So what error do you recieve with these objects? Quote Link to comment Share on other sites More sharing options...
andy_b42 Posted February 9, 2009 Author Share Posted February 9, 2009 Fatal error: Call to a member function Query() on a non-object Quote Link to comment Share on other sites More sharing options...
trq Posted February 9, 2009 Share Posted February 9, 2009 The only thing I can think of that might be happening is that your Database class isn't being included properly. I would use require_once instead of include. You might also turn display errors on and set error reporting to E_ALL. Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted February 9, 2009 Share Posted February 9, 2009 Look in your User class in the function load you have this $user = mysql_fetch_array($db->getResult()); change $db to $this->db also use __construct() instead of User() it fits tidier into php5 syntax Quote Link to comment Share on other sites More sharing options...
andy_b42 Posted February 9, 2009 Author Share Posted February 9, 2009 Look in your User class in the function load you have this $user = mysql_fetch_array($db->getResult()); change $db to $this->db Thanks, that line was like that becuase the only way i can make this work was to create the $db object outside the calss and then inside the function, use global $db and the call it as though $db were a variable local to that method. Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted February 9, 2009 Share Posted February 9, 2009 Using a global db would be better since you wouldnt have to instantie the db object multiple times. you could do this in your constructor public function __construct(){ global $db; $this->db=$db; } Quote Link to comment Share on other sites More sharing options...
andy_b42 Posted February 9, 2009 Author Share Posted February 9, 2009 i was under the impression that global variables are not very secure. Also, is there a chance that if i use 2 or more objects on the same page, the $db variables (for simplicity sake i would like to call it the same thing in every object) would get mixed up? It is quite important that each instance of the $db variable is unique to the object becasue the Database class holds the results of the query which are then used by the object if they are required. Quote Link to comment Share on other sites More sharing options...
andy_b42 Posted February 9, 2009 Author Share Posted February 9, 2009 my sincere apologies for wasting everyones time... I fell like a complete idiot, in the User constructor i had called the load() before creating the $db object Original: public function User($intID = 0){ if($intID > 0){ $this->load($intID); }else{ //echo "No " . $this->currentClass . "ID information supplied"; } try{ $this->db = new Database(); } catch(Exception $exc){ echo "Error creating database object"; } } Should be: public function User($intID = 0){ try{ $this->db = new Database(); } catch(Exception $exc){ echo "Error creating database object"; } if($intID > 0){ $this->load($intID); }else{ //echo "No " . $this->currentClass . "ID information supplied"; } } 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.