PHP 类名中的点
在 PHP 中拥有一个名为 I am getting 错误的类是否非法
class foo.bar{
}
,其中显示 {
Expected 而不是 .
是否有解决此问题的配置方法,或者错误是否在谈论某些内容别的?
Is it illegal in PHP to have a class named
class foo.bar{
}
I am getting errors that say {
expected instead of .
is there a configuration work around to this or is the error talking abouts something else?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
来自手册:
点无效,您无法更改任何设置以使它们有效。
From the manual:
Dots are not valid and you can't change any settings to make them valid.
类名称中不允许使用点。时期。
Dots are not allowed in class names. Period.
PHP 类名中不能包含句点。 没有绕过这个。
PHP class names can't have periods in them. There's no way around this.
点
.
是字符串连接运算符,因此不允许在标识符中的任何位置使用。The dot
.
is the string-concatenation operator, thus its not allowed anywhere in an identifier.不允许使用点,如文档所示: http://php.net/manual/en /语言.oop5.basic.php
A dot isnt allowed, as documented: http://php.net/manual/en/language.oop5.basic.php
不允许使用点。
类名可以是任何有效的标签,但不是 PHP 保留字。有效的类名以字母或下划线开头,后跟任意数量的字母、数字或下划线。作为正则表达式,它可以表示为:[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*。
Shamelesse 摘自此处。
Dots are not allowed.
The class name can be any valid label which is a not a PHP reserved word. A valid class name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*.
Shamelesse ripped from here.
是的,类名和命名空间中不允许使用点,正如下面的许多答案所说。但是,如果您非常需要使用点并且没有其他适合您的情况,那么请使用“U+2024”。基本上它看起来几乎是相同的点,但被认为是字母。
注意 1:此解决方案不是一个好的做法,因此将其作为最后的手段。
注 2:您可以从 IDE 收到有关使用此字符的警告,但您可以隐藏它。
注意3:我不推荐使用它,不要羞辱我。
Yes, dots are not allowed in class name and namespaces, as many of the answers below say.. However if you critically need to use dots and nothing else is appropriate for your case, then use "U+2024". Basically it's almost the same looking dot, but considered to be letter.
Note 1: This solution is not good practice, so use it as a last resort.
Note 2: You can get warning from your IDE about using this character, but you can just hide it.
Note 3: I am not recommending to use it, do not shame me.