开发者

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

开发者 https://www.devze.com 2023-01-26 00:12 出处:网络
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'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 cons开发者_开发百科tants 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.


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.


I rolled my own MVC setup. (Still going strong. Might open source it). The way I do it is to have an init script that passes all those constants to a controlling class.

The controlling class is a Singleton. So anytime another class needs access to it, it just gets the existing instance of the controlling class and all the variables are available from it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号