简体   繁体   English

为什么我的 Code Igniter controller 出现意外的 T_VARIABLE 错误?

[英]Why am I getting an unexpected T_VARIABLE error in my Code Igniter controller?

I have this controller in Code Igniter that begins with我在 Code Igniter 中有这个 controller,它以

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).我在以'importantValueToPassToViews' (第三行)开头的行中收到错误。

Parse error: syntax error, unexpected T_VARIABLE

Why?为什么?

Because you can't call a function in a class property definition.因为您不能在 class 属性定义中调用 function。 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();
    }
    // ...
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM