Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to handle config variable in MVC setup (PHP)

I'm writing my first basic bare bones MVC patterns setup in PHP. I know global variables are bad, and I aslo know I don't want all of my classes to have access to all of my config vars.

I have a settings.php file that I would like to define a bunch of constants in, like my db connection info, directory structure info, email addresses and so on. It will be one centralized location that holds all of my important information.

What's the best way to implement a config class so that each of my base classes for my controller and model only have access to the config vars they need? For example, my model base class should have access to by db connection info.

Basically I am just asking how anybody whole rolls their own MVC setup handles config information without declaring global variables, like we used to back in the procedural days.

Thanks.

like image 870
imns Avatar asked Nov 05 '22 06:11

imns


1 Answers

You're going to get a bunch of answers on this as it basically boils down to preference.

Personally, ive used a config array. example:

$conf['db']['username'] = "username";
$conf['db']['password'] = "password";

Then just pass byref the pieces you need into where they need to go.

like image 138
castis Avatar answered Nov 09 '22 04:11

castis