试图创建一个名为"List"的类,但list()函数正在打破它

Ble*_*rer 7 php class function case-sensitive

class List {
  public function hello()
  {
    return "hello";
  }
}

$list = new List;

echo $list::hello();
Run Code Online (Sandbox Code Playgroud)

给出错误:

PHP Parse error: syntax error, unexpected 'List' (T_LIST), expecting identifier (T_STRING) in /home/WtGTRQ/prog.php on line 3
Run Code Online (Sandbox Code Playgroud)

将"List"更改为"Lizt"可以解决问题.

我遗憾地理解Php函数不区分大小写,但我真的不想让List对象成为Lizt对象...有没有办法在没有重命名我的类的情况下绕过它?

Nik*_*ola 20

List 是一个受限制的PHP单词.

You cannot use any of the following words as constants, class names, function or method names.

http://www.php.net/manual/en/reserved.keywords.php

-

要回答您的问题,您必须将您的list班级名称更改为其他名称.MyList,CarList,Listing,等.

  • 十分感谢.关闭词库. (5认同)
  • @Josh H 这是有道理的:`注意:函数名称不区分大小写,尽管在声明中调用函数通常是一种很好的形式。`来自这里:http://fr.php.net/manual/en /functions.user-defined.php (2认同)