【问题标题】:unexpected '$twitter_handles' (T_VARIABLE), expecting function (T_FUNCTION) (Declaring array in Laravel controller)意外的 '$twitter_handles' (T_VARIABLE),期待函数 (T_FUNCTION) (在 Laravel 控制器中声明数组)
【发布时间】:2014-05-20 14:27:10
【问题描述】:

我试图在 Laravel 4.1 的控制器中声明一些数组,我收到错误:

syntax error, unexpected '$twitter_handles' (T_VARIABLE), expecting function (T_FUNCTION)

代码如下:

<?php

    class MiscController extends BaseController {

    $twitter_handles = array();
    $facebook_ids = array();

    $twitter_handles = array("@hello", "@there", "@you");

    $facebook_ids = array("122424", "12526", "123123");


        public function make_artists() {


            $length = sizeof($twitter_handles);

            $result = array();

            $i = 0;

            while($i < $length) {
                $result[$i]['fbid'] = $twitter_handles[$i];
                $result[$i]['twitter'] = $facebook_ids[$i];

                $i++;
            }

            echo $result;

        }

    }

我在制作控制器后已经使用了 dump-autoload。我不知道为什么我会收到错误消息。谢谢您的帮助。

【问题讨论】:

  • 试试 var $twitter_handles = array();

标签: php arrays laravel laravel-4


【解决方案1】:

Class properties 必须是...

... 使用关键字 publicprotectedprivate 之一定义,后跟普通变量声明。

例如...

class MiscController extends BaseController {

    private $twitter_handles = ["@hello", "@there", "@you"];
    private $facebook_ids = ["122424", "12526", "123123"];

要在你的类方法中使用它们,你需要在它们前面加上 $this 变量,例如

$length = sizeof($this->twitter_handles);

我强烈建议你read the fine manual

【讨论】:

    猜你喜欢
    • 2011-09-23
    • 2013-12-25
    • 2013-12-15
    • 2012-05-11
    • 2020-05-22
    • 2013-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多