我试图在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。我不知道为什么我收到错误。谢谢您的帮助。
答案 0 :(得分:3)
Class properties必须是......
...使用关键字 public , protected 或 private 之一定义,然后是普通变量声明。
例如......
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。