php8.1-枚举应该在自己的文件中吗?

发布于 2025-02-12 12:01:47 字数 1169 浏览 0 评论 0原文

我想开始使用枚举本地支持,但是我很难弄清楚如何组织和实施它们。在我看来,它们更像是结构良好的班级常数。例如,我有一个名称空间,例如:

 - plugins\
   - Base
   - Products
   - Orders
   - Connector
   - pluginX\
     - Base
     - Connector
     ...
   - pluginY\
     - Base
     - Connector
     ...

Connector是一个卷曲包装类,可用于对插件进行特定的调用。它具有一个抽象类,具有一些常数,例如const get ='get'const post ='post'和基本函数,例如request> request哪个采用$方法 - 有效的HTTP方法的字符串。其他连接器类具有更具体的功能。

对我来说,抽象基类似乎是一个简单的枚举方法的绝佳候选者,该方法强制执行5种有效方法之一。但是,如果我尝试像以下内容那样组织它:

<?php

namespace Plugins;

enum Method: string
{
    case GET = 'GET';
    case PUT = 'PUT';
    case POST = 'POST';
    case PATCH = 'PATCH';
    case DELETE = 'DELETE';
}

abstract class Connector
{
    ...
}

我会收到IDE警告,该文件在此文件中声明了另一个类。因此,看来枚举被视为类,将它们包括在与其他类可能违反PSR-4标准或其他类似的文件中。

那么这些枚举应该都生活在自己的课堂上吗?如果是这样,我不确定如何组织它们,因为它们可能不会整齐地融入命名空间,并且B.似乎我的项目开始被所有这些仅是单个enums 。

组织这些的最佳方法是什么?

I want to start making use of enumerations in PHP now that they are natively supported, but I'm having trouble figuring out how to organize and implement them. In my mind, they're more of a well-structured class constant. For instance I have a namespace like:

 - plugins\
   - Base
   - Products
   - Orders
   - Connector
   - pluginX\
     - Base
     - Connector
     ...
   - pluginY\
     - Base
     - Connector
     ...

The Connector is a Curl Wrapper class that can be used to make calls specific to the plugin. It has an abstract class that has some constants like const GET = 'GET', const POST = 'POST' and base functions like request which takes $method -- a string of a valid HTTP method. The other Connector classes extend that with more specific functionality.

To me, the abstract base class seems like a great candidate for a simple enum METHOD that enforces one of the 5 valid methods. However, if I try organizing it like the following:

<?php

namespace Plugins;

enum Method: string
{
    case GET = 'GET';
    case PUT = 'PUT';
    case POST = 'POST';
    case PATCH = 'PATCH';
    case DELETE = 'DELETE';
}

abstract class Connector
{
    ...
}

I get an IDE warning that Another class is declared in this file. So it seems like Enums are treated as classes, and including them in the same file as other classes might violate a PSR-4 standard or something.

So should these enums all be living in their own classes? If so, I'm left unsure how to organize them since A. they may not fit neatly into the namespace and B. seems like my project would start to get littered by all these little files that are just single enums.

What's the best way to organize these?

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

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

发布评论

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

评论(1

孤星 2025-02-19 12:01:47

是的,它们是对象,被剥离的类,您应该将它们放在自己的文件中。

在此RFC中阅读有关它们的更多信息: https://wiki.php.net/rfc/rfc/enumerations

yes, they are objects, stripped classes, you should put them in their own files.

read some more about them in this RFC: https://wiki.php.net/rfc/enumerations

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