开发者

MVC, how do I write my config file?

I've decided to rewrite my uploader using the MVC design pattern because my code was messy. The files are organized that way:

-model.php: Basically, it's a class that has all the functions needed to retrieve data from my database, such as the last uploaded files, the number of files the current user has uploaded, etc. There's a private variable called db_link that keeps the resource created by PDO (the PDO object).

-index.php: It's the controller. It checks if there's a file being uploaded, checks if it has a valid extension, etc. It calls functions from the class contained in model.php (I create an object of this class first).

-settings.php: My config file. This is where I determine my database username, password, also the max file size, the allowed extensions, etc. I simply do the following: $allowed_extension = array('swf', 'txt', 'jpg', 'gif', 'png');

But, if I include it in index.php and try to use this variable in a function, it doesn't work, because it isn't in the same scope (unless I include it inside the function itself, but I don't want to do that). I just need a better way to organize my settings.

Also, I create a db object with the class I talked about earlier. I n开发者_如何学编程eed to pass the object to the function I want to call, which is a pain in the neck too. Is there another way I could do that?


You probably should use constants in settings.php:

  • you can store an array using serialize method (see answer for PHP Constants Containing Arrays? )
  • constants are available everywhere
  • it is more readable
  • settings should always be constant

As for db, I am always using a singleton pattern myself, and then just using Model_Mysql::getInstance();, which makes it automatically available everywhere.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜