Welcome Guest, Not a member yet? Register   Sign In
PHP: accessing default values set in constructor
#1

[eluser]mjstenbe[/eluser]
Hi all,

I've been using CI's pagination class and tried to move all the configuration stuff within the constructor of the controller class, so that it would be easily available in every function. However I cant seem to access them properly afterwards. Any ideas whats wrong? See below:

Code:
function __construct()
    {

    parent::Controller();

    $this->load->helper('html');
    $this->load->library('user_agent');
    $this->load->library('pagination');

        $config['per_page'] = '10';
    $config['num_links'] = 1;

    }
    
    
    function index()
    {    
         // Neither of these will return me proper data - how to access the array or the actual values?
         $somedata = $this->config['per_page'];
         $somedata2 = $this->config;
        
        }
#2

[eluser]danmontgomery[/eluser]
Code:
function __construct() {
    parent::Controller();

    $this->load->library('pagination');

    $this->config['per_page'] = 10;
}

function index() {
    $somedata = $this->config['per_page'];
}
#3

[eluser]mjstenbe[/eluser]
Thanks for your reply. However, Its still missing something since using your code Im getting:

"Message: Undefined variable: config"

I thought I could declare the config array in the constructor. I even tried declaring it as a class variable, but it didnt work. Feeling a bit puzzled at the moment. Any ideas?

Thanks,
Mika




Theme © iAndrew 2016 - Forum software by © MyBB