2

When I use require_once and try to access the variable within the required file I get an error that the variable ($db) is undefined.

I have tried to place the require_once statement several diffrent places because I thought it might be variable scope issues, but at this point I try to call it within the constructor and the assign the value to a class variable.

My config class:

class Config {

    public $db;

    public function __construct() {
        require_once (BASE_DIR."config/DBConfig.php");
        $this->db = $db;
    }
}

The DBConfig file looks like this:

$db['default']['hostname'] = 'localhost'
hakre
  • 193,403
  • 52
  • 435
  • 836
Lasse Vabe Rolstad
  • 602
  • 2
  • 9
  • 20
  • Is your DBConfig file literally just that? No ` – deceze Feb 02 '12 at 12:58
  • Look at 1: http://stackoverflow.com/questions/9002188/require-once-to-global-scope-within-a-function, 2: http://stackoverflow.com/questions/2679602/what-is-the-scope-of-require-once-in-php – Ozair Kafray Feb 02 '12 at 13:00
  • I actually solved it by making Config class singelton. It seems that since Require_once only gets the file the first time the class gets created the variable is only available in the first object i create. – Lasse Vabe Rolstad Feb 02 '12 at 13:03
  • @LasseVabeRolstad: That is the normal behaviour of `require_once`. It requires only once. Hence the name. See as well `require` and please add your "solution" as an answer below and accept it (well actually I'd say Luc Franken has the answer). Just setup what you need upfront before you use it and then you don't have these problems. And yes, Singleton is part of your problem, not the solution. – hakre Jun 02 '12 at 13:47

2 Answers2

8

I would do it differently and use dependency injection for this, giving you:

public function __construct((IDb)$Db) {
    $this->db=$Db;
}

That way it is way easier to test your software later, it is clear that this class needs some database object. Which makes your code also less coupled which seems to be a good architectural decision.

Also it would make it much easier to start using for example a different database class which saves you later time and effort.

muttley91
  • 12,278
  • 33
  • 106
  • 160
Luc Franken
  • 2,994
  • 1
  • 17
  • 14
2

It seems that since Require_once only gets the file the first time the class gets created the variable is only available in the first object i create.

Well then, change require_once to require. require_once is for including files that define classes and functions, that a) don't need to be included more than once and b) would cause errors if they were included more than once. If you always want to include the file, skip the _once.

Having said that, including a database configuration more than once is a sign of bad design.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • Hmm, this will solve the problem. However, it seems that the OP is using `require_once` to avoid bad architecture problems, which is not the way the method should be used. – sitilge Jun 24 '16 at 21:04