Proper way to use a config file?

Posted by user156814 on Stack Overflow See other posts from Stack Overflow or by user156814
Published on 2010-04-12T18:48:33Z Indexed on 2010/04/12 18:53 UTC
Read the original article Hit count: 447

Filed under:
|
|

I just started using a PHP framework, Kohana (V2.3.4) and I am trying to set up a config file for each of my controllers.

I never used a framework before, so obviously Kohana is new to me. I was wondering how I should set up my controllers to read my config file.

For example, I have an article controller and a config file for that controller. I have 3 ways of loading config settings

// config/article.php
$config = array(
    'display_limit'         => 25, // limit of articles to list
    'comment_display_limit' => 20, // limit of comments to list for each article
    // other things
);

Should I

A) Load everything into an array of settings

// set a config array
class article_controller extends controller{

    public $config = array();

    function __construct(){
        $this->config = Kohana::config('article');
    }       
}

B) Load and set each setting as its own property

// set each config as a property
class article_controller extends controller{

    public $display_limit;
    public $comment_display_limit;

    function __construct(){
        $config = Kohana::config('article');

        foreach ($config as $key => $value){
            $this->$key = $value;
        }
    }
}

C) Load each setting only when needed

// load config settings only when needed
class article_controller extends controller{

    function __construct(){}

    // list all articles
    function show_all(){
        $display_limit = Kohana:;config('article.display_limit');
    }

    // list article, with all comments
    function show($id = 0){
        $comment_display)limit = Kohana:;config('article.comment_display_limit');
    }
}

Note: Kohana::config() returns an array of items.

Thanks

© Stack Overflow or respective owner

Related posts about php

Related posts about kohana