PHP 中是否可以将常量命名为受保护的字?

发布于 2024-10-01 12:07:19 字数 162 浏览 1 评论 0原文

在 C# 中,变量和其他事物可以通过在名称前添加 @ 符号来命名为受保护的名称,例如“类”。所以,@class 是一个有效的名称。在 PHP 中可以做同样的事情吗?我使用一类常量来模拟 HTML 属性(例如 ID 和 Class)的枚举。现在我使用“CssClass”,但我宁愿以某种方式使用名称 Class。

In C#, variables and other things can be named protected names such as "class" by prepending the name with an @ sign. So, @class is a valid name. Is it possible to do this same thing in PHP? I am using a class of constants to simulate an enum for HTML attributes such as ID, and Class. For now I am using "CssClass" but I'd rather use the name Class somehow.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

神魇的王 2024-10-08 12:07:19

不,不可能,至少对于类常量来说是不可能的。

您不能使用以下任何[保留]字作为常量、类名称、函数或方法名称。

我不了解 C#,但 PHP 中没有任何特殊符号可以将关键字转换为标识符。只要您不将其命名为与关键字完全相同(字母大小写除外),它就只是任何普通的常量名称。

一个(不同,因为它不仅仅是CSS)前缀怎么样?打字重复,但这是一个很好的解决方法。我意识到,如果您的类被命名为类似 HTMLAttribute 的名称,这也可能是多余的,但这是最简单的方法。

const A_ID = 'id';
const A_CLASS = 'class';
// etc

Nope, not possible, at least not for class constants.

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

I don't know about C#, but there isn't any special symbol in PHP to transform a keyword into an identifier. As long as you don't name it exactly the same as a keyword (barring letter case), it'll just be any normal constant name.

How about a (different since it's not just CSS) prefix? Gets repetitive to type, but is a nice workaround. I realize this may be redundant as well if your class is named something like HTMLAttribute, but it's the easiest way out.

const A_ID = 'id';
const A_CLASS = 'class';
// etc
场罚期间 2024-10-08 12:07:19

是的,这是可能的。

事实上,您可以将任何内容定义为常量:

define("define", 1);
define("class", 1);
define("if", 1);
define("=.+*", 1);

但是,您不能使用所有已定义的常量。

您可以再次使用 constant("if") 查询它们。但这并不完全是您所要求的。因此,与 C# 不同,没有使用任何随机常量的快捷方式。但至于命名,几乎没有任何限制。 (不过可能是一个错误。它是 PHP。)

Yes, it is possible.

In fact you can define anything as constant:

define("define", 1);
define("class", 1);
define("if", 1);
define("=.+*", 1);

However, you can not use all defined constants.

You can query them with constant("if") again. But this is not exactly what you asked for. So unlike C# there is no shortcut to use any random constant. But as for naming them, there are almost no restrictions. (Might be a bug though. It's PHP.)

人│生佛魔见 2024-10-08 12:07:19

常量

常量的名称遵循与 PHP 中任何标签相同的规则。有效的常量名称以字母或下划线开头,后跟任意数量的字母、数字或下划线。作为正则表达式,它的表达方式如下:[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*

保留关键字列表

这些词在 PHP 中具有特殊含义。其中一些表示看起来像函数的东西,一些看起来像常量,等等——但实际上它们不是:它们是语言构造。 不能使用以下任何单词作为常量、类名、函数或方法名。

[在此处查看列表]

在这些规则中,您可以自由地命名您的名字。例如,您可以命名一个常量 _CLASS,但不能命名为 CLASS。我会避免使用此类不明确的名称和应用程序特有的命名空间常量,例如 MYAPP_CLASS 。

Constants:

The name of a constant follows the same rules as any label in PHP. A valid constant name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thusly: [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*

List of reserved keywords:

These words have special meaning in PHP. Some of them represent things which look like functions, some look like constants, and so on--but they're not, really: they are language constructs. You cannot use any of the following words as constants, class names, function or method name.

[see list here]

Within these rules you're free to make up your names. So, for instance, you could name a constant _CLASS, but not CLASS. I'd avoid the use of such ambiguous names though and namespace constants that are particular to the app, like MYAPP_CLASS.

离鸿 2024-10-08 12:07:19

从 PHP5 到 PHP7,类常量几乎可以命名为任何名称:

class ReservedWord
{
    // Works in PHP >= 7.0 only
    const NULL = null;
    const TRUE = true;
}

但是,多亏了 this手册的一部分此评论,我发现类常量不能被命名为以下几件事(请参阅测试此处):

  1. class
  2. static
  3. __halt_compiler (哦,这太有用了!)

编辑:正如我在 在 RFC 中,class 常量不起作用的原因是名称解析<代码>::类。然而,对于另外两个人,仍然一无所知。

Going from PHP5 to PHP7, a class constant could be named almost anything:

class ReservedWord
{
    // Works in PHP >= 7.0 only
    const NULL = null;
    const TRUE = true;
}

However, thanks to this part of the manual and this comment, I've found that a class constant cannot be named these few things (see the test here):

  1. class
  2. static
  3. __halt_compiler (oh, that was so useful!)

Edit: As I found in here in an RFC, the reason why class constant does not work is the name resolution ::class. However, still no idea about the two others.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文