MelusDecus
MelusDecus

Reputation: 81

Codeigniter model class variable

I'm just getting an error doing this, and i can't understand why.

class Budget_model extends CI_Model
{

    // Predefine global class vars
    private $current_date = date('j');  // Current day date
    private $current_month = date('n'); // Current month date
    private $current_year = date('Y');  // Current year

}

This would just give me this error,

Parse error: syntax error, unexpected '(', expecting ',' or ';' in /Applications/MAMP/htdocs/therace/application/models/budget_model.php on line 7

But why? How can i fix this issue?

Upvotes: 0

Views: 177

Answers (1)

MrCode
MrCode

Reputation: 64526

Properties can't be initialised like that, you need to do it in the constructor:

private $current_date;

public function __construct()
{
    $this->current_date = date('j');
}

The class is a blueprint and its property definitions need to be independent of any runtime variables or functions.

Upvotes: 4

Related Questions