命名约定或目录结构
我是PHP程序员,但我想讨论的是受Java的影响。
在阅读了一些有关匈牙利表示法和几种语言的命名约定的文章后,我证明了Java的接口命名约定是多么清晰,我有一个问题。
考虑到对象集合的情况,我们有一个名为 AbstractCollection 的抽象类和名为 Lists 的具体类,我应该如何命名一个 Interface 列表?
问题是,在Java的命名约定中,接口没有前缀或后缀,例如ListsInterface或InterfaceLists。
Lists 是一个接口名称,显然,至少在 PHP 中,同一目录中的两个文件不能具有相同的类或接口名称。
我知道,并不是每个类都需要接口,但是如果我决定转到编程团队怎么办?其他程序员可能会使用我的类,但他们不应该打开类文件来查看它提供了哪些公共方法。
只要阅读它的界面,一切就一目了然了。
目录结构点来自于添加与接口同名的子目录以及在其中添加具体类文件的可能性。例如:
/
/Collections
/Collections/Lists.php <-- Interface
/Colection/Lists/Lists.php <--- Concrete Class
现在怎么办?
I'm PHP programmer, but what I want to discuss is influenced by Java.
After read some articles about Hungarian Notation and Naming Conventions of several Languages, I proved how clear are the Java's Naming Convention for Interfaces, I have one question.
Considering the Object Collections' case, where we have an Abstract Class called AbstractCollection and the concrete class called Lists, how should I name an Interface for Lists?
The question is, in Java's Naming Conventions, there is no prefix nor suffix for Interfaces, such as ListsInterface or InterfaceLists.
Lists IS an Interface name and, obviously, at least in PHP, two files in same directory cannot have the same name of a class or interface.
Not EVERY class needs an Interface, I know, but what if I decide to move to a programming team? The other programmers may use my classes, but they should not open the class files to see what public methods it offers.
Just by reading its Interface, everything is clear.
The Directory Structure point came from the possibility to add a subdirectory with the same name of the Interface and, inside it, the concrete class' file. E.g.:
/
/Collections
/Collections/Lists.php <-- Interface
/Colection/Lists/Lists.php <--- Concrete Class
What now?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不应为接口和类指定相同的名称。绝不。为了您的读者。
我认为您的特定问题与约定无关,但由于具体的命名问题
Lists
听起来根本不像一个接口。List
确实如此。但在这种情况下,实现类将有一个更具体的名称,例如List_DoublyLinked
和List_SinglyLinked
(或您正在谈论的任何类型的列表)。You shouldn't give an interface and a class the same name. Never. For your readers sake.
I think your particular problem has little to do with convention, but with a concrete naming problem
Lists
doesn't sound like an interface at all.List
does. But in that case an implementing class would have a more concrete name, likeList_DoublyLinked
andList_SinglyLinked
(or whatever kind of list you are talking about).我认为你应该检查 PHP 命名约定而不是 Java。
http://framework.zend.com/manual/en/coding -standard.naming-conventions.html
Zend 提供了一个很好的方案,几乎考虑了所有情况。
Symfony 有另一个( http://trac.symfony-project.org/wiki/HowToContributeToSymfony#CodingStandards ),Pear 还有另一个( http://pear.php.net/manual/en/standards.php ) 。
在您的例子中,只是为了表明接口是接口,Zend 强制您在接口上添加后缀。所以你的目录结构应该是这样的:
我不知道这篇文章是否回答了你的问题,但我希望它有帮助。
I think you should check PHP naming conventions instead of Java.
http://framework.zend.com/manual/en/coding-standard.naming-conventions.html
Zend provides a good one, nearly considering all the cases.
Symfony has another ( http://trac.symfony-project.org/wiki/HowToContributeToSymfony#CodingStandards ), Pear has another ( http://pear.php.net/manual/en/standards.php ) too.
In your case just to show that an interface is an interface, Zend forces you to put a suffix on your interafces. So your dir structure should be like:
I don't know if this post answers your question but I hope that it helps.