PHP 5.3 中接口/抽象类的命名(使用命名空间)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1150098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:14:49  来源:igfitidea点击:

Naming of interfaces/abstract classes in PHP 5.3 (using namespaces)

phpnamespacesnaming-conventions

提问by Philippe Gerber

Prior to PHP 5.3 I used to name interfaces/abstract classes like this:

在 PHP 5.3 之前,我曾经这样命名接口/抽象类:

abstract class Framework_Package_Subpackage_Abstract {}
Framework/Package/Subpackage/Abstract.php

interface Framework_Package_Subpackage_Interface {}
Framework/Package/Subpackage/Interface.php

Now with PHP 5.3 and using namespaces I can't use my convention anymore, because interfaceand abstractare reserved keywords.

现在使用 PHP 5.3 并使用命名空间,我不能再使用我的约定,因为interfaceandabstract是保留关键字。

namespace Framework\Package\Subpackage;
abstract class Abstract {}
Framework/Package/Subpackage/Abstract.php

namespace Framework\Package\Subpackage;
interface Interface {}
Framework/Package/Subpackage/Interface.php

So, what should I name my classes/interfaces like?

那么,我应该如何命名我的类/接口?

采纳答案by Mike Graf

A current coding guide "PSR-2" basically suggests you drop the interface down one directory and combine the name.

当前的编码指南“PSR-2”基本上建议您将界面放到一个目录下并组合名称。

eg:

例如:

File \Vendor\Foo\Interface.php ===> \Vendor\FooInterface.php.

and the use stament eg:

和使用声明,例如:

use \Vendor\Foo\Interface; ===> use\Vendor\FooInterface;

see: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md

见:https: //github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md

回答by Pascal MARTIN

About this question (Abstract and Interface), you might have a look at the post "Migrating OOP Libraries and Frameworks to PHP 5.3" on Matthew Weier O'Phinney's blog -- it's about Zend Framework, and how they could solve that problem in 2.0.

关于这个问题(抽象和接口),您可能会在 Matthew Weier O'Phinney 的博客上查看“将OOP 库和框架迁移到 PHP 5.3”一文——它是关于 Zend Framework,以及他们如何在 2.0 中解决该问题.

One of the things they note is :

他们注意到的一件事是:

In other OOP languages, such as Python, C#, interfaces are denoted by prefixing the interface with a capital 'I'; in the example above, we would then have Zend::View::IView.

在其他 OOP 语言(例如 Python、C#)中,接口通过在接口前加上大写的“I”来表示;在上面的例子中,我们会有 Zend::View::IView。

So, in your example, you would have something like this, I guess :

所以,在你的例子中,你会有这样的事情,我猜:

namespace Framework\Package\Subpackage;
abstract class ASubpackage {}
Framework/Package/Subpackage/ASubpackage.php

namespace Framework\Package\Subpackage;
interface ISubpackage {}
Framework/Package/Subpackage/ISubpackage.php

What do you think about that ? (I have not tested this way, but it doesn't look like a bad idea ? )

你怎么看? (我没有测试过这种方式,但它看起来不是一个坏主意?)

回答by jason

I personally would recommend avoiding any usage of Hungarian Notation, and consider following the Java standard for interface names; that is, they're named descriptively just like any other class. See this SO questionfor a discussion of the ins and outs of Hungarian notation.

我个人建议避免使用匈牙利表示法,并考虑遵循接口名称的 Java 标准;也就是说,它们的名称与任何其他类一样具有描述性。有关匈牙利符号的来龙去脉的讨论,请参阅此 SO 问题

A good example of using generic, descriptive names indicative of functionality or behavior can be found in PHP's own SPL, like: "Countable", "Iterator", "ArrayObject".

在 PHP 自己的 SPL 中可以找到使用通用的、描述性的名称来指示功能或行为的一个很好的例子,例如:“Countable”、“Iterator”、“ArrayObject”。

回答by erenon

SubpackageAbstract, SubpackageInterface

子包抽象,子包接口

回答by Stoutie

You could also do something like this:

你也可以做这样的事情:

src/Chess/Piece.php

源代码/国际象棋/Piece.php

<?php

namespace \Chess;

abstract class Piece implements \Chess\PieceInterface {}

src/Chess/PieceInterface.php:

src/Chess/PieceInterface.php:

<?php

namespace \Chess;

interface PieceInterface {}

src/Chess/Piece/Pawn.php:

src/Chess/Piece/Pawn.php:

<?php

namespace \Chess\Piece;

class Pawn extends \Chess\Piece {}

Here's how I would setup autoloading in composer.json

这是我在composer.json 中设置自动加载的方法

{
    "autoload": {
        "psr-0": {
            "": "src/"
        }
    }
}

回答by blockhead

Honestly, I believe the Hungarian notation was introduced with C#, because there is not "extends" and "implements" keyword like in Java. So to differentiate the convention became to call it IView. In Java, the interface would be called View alone, and implementations would be called DefaultViewImpl, SmartyViewImpl or something like that. Since, PHP doeshave extends and implements keywords, it makes sense to use the Java convention. I have heard the argument that the Hungarian notation does lend it self to making the API elements identifiable just by looking at the class names. In that case I would call it either IView or AbstractView.

老实说,我相信匈牙利符号是在 C# 中引入的,因为没有像 Java 中那样的“扩展”和“实现”关键字。因此,为了区分约定,将其称为 IView。在 Java 中,接口将单独称为 View,实现将称为 DefaultViewImpl、SmartyViewImpl 或类似的名称。因为 PHP确实有 extends 和 implements 关键字,所以使用 Java 约定是有意义的。我听说匈牙利表示法确实有助于仅通过查看类名就可以识别 API 元素。在这种情况下,我会称它为 IView 或 AbstractView。

回答by alexanderpas

In my opinion, the best way to solve this, is by simply appending Class to your classnames.

在我看来,解决这个问题的最好方法是简单地将 Class 附加到您的类名。

namespace Framework\Package\Subpackage;
abstract class AbstractClass {}
Framework/Package/Subpackage/AbstractClass.php

namespace Framework\Package\Subpackage;
interface InterfaceClass {}
Framework/Package/Subpackage/InterfaceClass.php

note that this is still not perfect (however, works perfectly) but i keeps the code similar to the original idea ;)

请注意,这仍然不完美(但是,工作得很好)但我保留了与原始想法相似的代码;)