【问题标题】:Codeigniter model class variableCodeigniter 模型类变量
【发布时间】:2012-11-22 10:41:19
【问题描述】:

我只是在执行此操作时遇到错误,我不明白为什么。

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

}

这只会给我这个错误,

解析错误:语法错误,意外 '(',期待 ',' 或 ';' in /Applications/MAMP/htdocs/therace/application/models/budget_model.php 在第 7 行

但是为什么呢?我该如何解决这个问题?

【问题讨论】:

  • 您是否尝试过在类构造函数中定义它们?

标签: php class codeigniter


【解决方案1】:

属性不能这样初始化,需要在构造函数中进行:

private $current_date;

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

类是一个蓝图,它的属性定义需要独立于任何运行时变量或函数。

【讨论】:

    猜你喜欢
    • 2011-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-19
    • 2019-02-23
    • 2011-10-12
    相关资源
    最近更新 更多