ngreenwood6 Posted November 17, 2009 Share Posted November 17, 2009 ok so I have defined 5 constants in my config.php file, one of which is a global: config.php //get the global user global $user; //define the user id define("USER_ID", $user->uid); //define the database settings define("DB_HOST", "host"); define("DB_DB", "db"); define("DB_USER", "user"); define("DB_PASS", "pass"); Now I create a class for my database and am trying to use those constants. All of them is working but the USER_ID. I dont know if it has something to do with it being a global but it is really annoying me. Here is how I am calling them in my db class class db { //database connection private $host = DB_HOST; private $db = DB_DB; private $user = DB_USER; private $pass = DB_PASS; private $user_id = USER_ID; } Any ideas on why this is happening. Any help is appreciated. Quote Link to comment Share on other sites More sharing options...
joel24 Posted November 17, 2009 Share Posted November 17, 2009 woops, misread your question. will reply soon Quote Link to comment Share on other sites More sharing options...
trq Posted November 17, 2009 Share Posted November 17, 2009 ok so I have defined 5 constants in my config.php file, one of which is a global: All constants are global and the global keyword makes no sense at all outside of a function. Where is $user defined? Quote Link to comment Share on other sites More sharing options...
joel24 Posted November 17, 2009 Share Posted November 17, 2009 is $user->uid calling a function or a variable... if its a variable check its public? otherwise if its a function check its returning the correct result... i'm assuming the "user" object is created before you define the constant (USER_ID)..? Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted November 17, 2009 Author Share Posted November 17, 2009 The $user is defined in a separate file. $user is a public variable. If I use the USER_ID global anywhere else in my code it works fine. Its just not working in my db class. Quote Link to comment Share on other sites More sharing options...
trq Posted November 17, 2009 Share Posted November 17, 2009 $user is a public variable. What exactly do you mean by that? Is it defined in a class with the public visibility operator? Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted November 17, 2009 Author Share Posted November 17, 2009 no I just meant that it was viewable. sorry wrong wording there. In my index.php page I have this: <?php include("config.php"); include("db.class.php"); ?> If I then echo USER_ID in there it displays it fine. But its not working when I set $user_id = USER_ID; in the db class. Quote Link to comment Share on other sites More sharing options...
trq Posted November 17, 2009 Share Posted November 17, 2009 But its not working when I set $user_id = USER_ID; in the db class. What code have you got that proves that? eg: How do you use $user_id within your class? Also, this here.... define("USER_ID", $user->uid); Suggests that $user is an object. Is it? Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted November 17, 2009 Author Share Posted November 17, 2009 Yes I believe that $user is an object, not sure because I dont know where it is located(its from a cms just using it for login). When I try to use $user_id in my class I use it by doing $this->user_id Quote Link to comment Share on other sites More sharing options...
trq Posted November 17, 2009 Share Posted November 17, 2009 Yes I believe that $user is an object, not sure because I dont know where it is located So, when I asked you earlier where $user was defined and you replied "The $user is defined in a separate file. $user is a public variable." you really meant you have no idea where it is defined? Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted November 17, 2009 Author Share Posted November 17, 2009 yeah pretty much but I know it is a variable, it is in a separate file, and it has to be public since I can access it lol. Any ideas why this isnt working? Quote Link to comment Share on other sites More sharing options...
trq Posted November 17, 2009 Share Posted November 17, 2009 but I know it is a variable, it is in a separate file, and it has to be public since I can access it lol. But its not a simple variable, according to how your using it its an object. Can you use var_dump on it just prior to where you try to assign one of its properties to an object? var_dump($user); You might also try finding where it is defined. Surely your IDE has a search function? Try searching for $user =. Any half decently written code wouldn't have an object like this floating around in the global scope. 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.