开发者

Dot in a PHP Class Name

开发者 https://www.devze.com 2023-03-08 11:43 出处:网络
Is it illegal in PHP to have a class named class foo.bar{ } I am getting errors that say { expected instead of . is ther开发者_运维问答e a configuration work around to this or is the error talki

Is it illegal in PHP to have a class named

class foo.bar{


}

I am getting errors that say { expected instead of . is ther开发者_运维问答e a configuration work around to this or is the error talking abouts something else?


From the manual:

A valid class name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.

Dots are not valid and you can't change any settings to make them valid.


Dots are not allowed in class names. Period.


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.


A dot isnt allowed, as documented: http://php.net/manual/en/language.oop5.basic.php


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.


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.

.   <- Regular dot
․   <- U+2024

namespace my\name\space\with.dot  (Error)
namespace my\name\space\with․dot  (Ok)

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消