简体   繁体   English

Php - 名为List的类的静态函数

[英]Php - Static function of class named List

I have a class named List, this class has a static method. 我有一个名为List的类,这个类有一个静态方法。 When I call: 我打电话的时候:

List::method();

The following error occurs: 发生以下错误:

syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM), expecting '('

Does php think I want to call the native function list()? php是否认为我想调用本机函数列表()? Do I have to rename the class? 我必须重命名课程吗?

Yes and no. 是的,不是。 list is not a function, but a language construct . list不是一个函数,而是一个语言结构 As such it's a 'reserved word', meaning it will be treated differently by the tokenizer during compilation. 因此它是一个“保留字”,这意味着编译期间令牌化器将对它进行不同的处理。

The compiler expects the list token to be followed as in the language construct, so with a ( as it reports. Instead you're feeding it the scope operator, meaning it gets confused. Easiest solution is to rename your class (it's overly generic anyway) or to prevent namespace conflicts, either by putting the class List in your own namespace, or by explicitly referencing the global namespace: 编译器期望跟随list标记,就像在语言结构中一样,因此使用a (因为它报告。相反,你正在向它提供范围操作符,这意味着它会变得混乱。最简单的解决方案是重命名你的类(无论如何它都是过于通用的)或者通过将类List放在您自己的命名空间中,或者通过显式引用全局命名空间来防止命名空间冲突:

\List::method();

This should work as well from PHP 5.3 upwards - it tells the compiler to look into the global namespace, where it automatically expects a class name after the backslash. 这应该从PHP 5.3向上工作 - 它告诉编译器查看全局命名空间,它会在反斜杠后自动期望一个类名。

Yes, PHP tries to call the native function 是的,PHP尝试调用本机函数
T_PAAMAYIM_NEKUDOTAYIM means unexpected double colon, so when you rename the class everything should be allright T_PAAMAYIM_NEKUDOTAYIM意味着意外的双冒号,所以当你重命名该类时,一切都应该是好的

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

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