Jump to content

class help


ngreenwood6

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/181828-class-help/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/181828-class-help/#findComment-958957
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/181828-class-help/#findComment-958968
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/181828-class-help/#findComment-959572
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.