0

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?

1
  • Have you tried defining them in a class constructor instead? Commented Nov 22, 2012 at 10:47

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.