【问题标题】:Trying to make a class called "List", but the list() function is breaking it试图创建一个名为“List”的类,但 list() 函数正在破坏它
【发布时间】:2013-09-18 14:40:31
【问题描述】:
class List {
  public function hello()
  {
    return "hello";
  }
}

$list = new List;

echo $list::hello();

给出错误:

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

将“List”更改为“Lizt”可以解决问题。

我很遗憾地理解 Php functions are case-insensitive,但我真的不想让 List 对象成为 Lizt 对象......有没有办法在不重命名我的类的情况下规避这个问题?

【问题讨论】:

  • MyList 或者更多关于列表内容的内容怎么样?
  • 简短回答:不。长答案:也没有。

标签: php function class case-sensitive


【解决方案1】:

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 类名更改为其他名称。 MyListCarListListing等。

【讨论】:

  • 这没有任何意义。 list 看起来是保留字,而不是 List
  • @Josh H 这很有意义:Note: Function names are case-insensitive, though it is usually good form to call functions as they appear in their declaration. 从这里:fr.php.net/manual/en/functions.user-defined.php
猜你喜欢
  • 2020-12-20
  • 1970-01-01
  • 1970-01-01
  • 2017-12-26
  • 2020-12-18
  • 1970-01-01
  • 1970-01-01
  • 2015-06-29
  • 1970-01-01
相关资源
最近更新 更多