duanran3115 2012-04-07 19:58
浏览 36
已采纳

为什么我的Code Igniter控制器出现意外的T_VARIABLE错误?

I have this controller in Code Igniter that begins with

class MyController extends CI_Controller {
    private $data = array(
        'importantValueToPassToViews' => $this->Animal->getPrey(),
    );
    ...

I am getting an error on the line beginning with 'importantValueToPassToViews' (the third line).

Parse error: syntax error, unexpected T_VARIABLE

Why?

  • 写回答

1条回答 默认 最新

  • dqn48247 2012-04-07 20:00
    关注

    Because you can't call a function in a class property definition. You can set it to a constant, or an array of constants.

    You'll need to do that in the constructor:

    <?php
    class MyController extends CI_Controller {
    
        private $data = array();
    
        public function __construct()
        {
            parent::__construct();
    
            $this->data['importantValueToPassToViews'] = $this->Animal->getPrey();
        }
        // ...
     }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效