class HTTP_Input extends HTTP_Protocol implements Interface_Readable_Context{}
然后您不需要事先包含,因为 new HTTP_Input 将自动加载以下内容:
HTTP/Input.php
HTTP/Protocol.php
接口/可读/Context.php
There are lot's of different ways to construct your system in regards to Class Names, Auto loading, Interfaces etc, what you have to remember is that if other people are going to be using the code, developing on it, you want to make it as simple and easy to learn otherwise developers will have a hard to coding it.
There are many ways as stated above, one of them is the PSR-0 which looks after Class Names, Namespaces and Directory Structure, it's a simple concept that is used by many of the large developers out there such as Zend and many others
Im working on a system at the moment and although it does not have the PSR-0 structure it is still pretty simple:
Also im not sure that you convention is apply able to libraries, I mean the file name has no relevance the the actual code, so combining the class name with parent and interfaces is pointless as your not ever going to include the file via the name of the class it extends.
if it's mainly the dependencies your interested in automatic solution to loading the classes dependencies then i would adopt the PSR-0 solution.
Interface \File
File.php
class \File\Text
File/Text.php
class \File\PHP
File/PHP.php
经常重构并检查您的代码;)。快乐编码;)
It's hard to answer such a question because it depends a lot on what you feel good for yourself. From my own practice I can only suggest to give natural and not technical names. I nearly totally dropped to use Interface or Abstract in my classnames, dropped using a short version like C, I or A in front of the name. I even now try to prevent such. Give the class a name that it deserves ;).
There is no problem to find out if something is an interface or a class itself is abstract. That's already in the language. So additionally putting that into the same classname (and that's a bit the same for parent classes, however you often naturally name child classes related) is just superfluous.
In the end you just use your classes by their name. So find good names instead of technical names is probably the bottom line. There is a nice chapter in the book called Clean Code about finding good names for your classes.
class Classname
Classname.php
Next to that, make use of namespaces (or pseudeo-namspaces in form of prefixes if you can't use them due to the PHP version) and place the classes into subdirectories and use one file = one class.
Interface \File
File.php
class \File\Text
File/Text.php
class \File\PHP
File/PHP.php
Refactor often and review your code ;). Happy coding ;)
I think best thing is PSR-4. Also PSR-0 deprecated.
Before I started developing my first serious project in PHP, I had done a lot of research and realized that the best practice was to follow the general standards (PSR). Of course we are quite free. In some cases, we can bend some parts for our own project.
There are no rules for naming files, except when you use a framework. You can feel free for naming your files just as you want, however, if you are using a framework, it can cause an error when trying to load the files.
The best implementation is the one you feel best when using.
发布评论
评论(5)
命名约定
文件名不应有任何前缀后缀 - 仅类名,以可以通过
自动加载器
。从不扩展的。通常的做法是添加(到类名中)诸如 Interface 或 Abstract 之类的单词,但绝对不要添加父母的名字。
Naming conventions
Names of files should not have any suffixes of prefixes - only name of class, to make it possible find them by
autoloader
.Never. It's a common practice to add (into name of class) words like Interface, or Abstract, but not names of parents, definitely.
在类名、自动加载、接口等方面构建系统有很多不同的方法,您必须记住的是,如果其他人要使用代码并在其上进行开发,您希望将其制作为简单易学,否则开发人员将很难对其进行编码。
如上所述,有很多方法,其中之一是 PSR-0 负责类名、命名空间和目录结构,这是一个简单的概念,被许多大型开发人员使用,例如
Zend
和许多其他我正在开发的项目目前的系统,尽管它确实没有 PSR-0 结构,它仍然非常简单:
您可以访问此处的源代码并浏览:https:// /github.com/AdminSpot/ASFramework
另外,我不确定您的约定是否适用于库,我的意思是文件名与实际代码无关,因此将类名与父级和接口结合起来是没有意义的你永远不会通过名称包含该文件它扩展的类。
如果您主要对加载类依赖项的自动解决方案感兴趣,那么我会采用 PSR-0 解决方案。
基本上,您执行以下操作:
像这样构造您的类:
然后您不需要事先包含,因为
new HTTP_Input
将自动加载以下内容:There are lot's of different ways to construct your system in regards to Class Names, Auto loading, Interfaces etc, what you have to remember is that if other people are going to be using the code, developing on it, you want to make it as simple and easy to learn otherwise developers will have a hard to coding it.
There are many ways as stated above, one of them is the PSR-0 which looks after Class Names, Namespaces and Directory Structure, it's a simple concept that is used by many of the large developers out there such as
Zend
and many othersIm working on a system at the moment and although it does not have the PSR-0 structure it is still pretty simple:
You can visit the source here and take a browse: https://github.com/AdminSpot/ASFramework
Also im not sure that you convention is apply able to libraries, I mean the file name has no relevance the the actual code, so combining the class name with parent and interfaces is pointless as your not ever going to include the file via the name of the class it extends.
if it's mainly the dependencies your interested in automatic solution to loading the classes dependencies then i would adopt the PSR-0 solution.
Basically you do the following:
Structure your classes like so:
and then you do not need to include before hand, as
new HTTP_Input
will auto load the following:很难回答这样的问题,因为这很大程度上取决于你对自己的感觉如何。根据我自己的实践,我只能建议使用自然名称而不是技术名称。我几乎完全放弃在类名中使用 Interface 或 Abstract,放弃在名称前面使用诸如
C
、I
或A
之类的简短版本。我什至现在都在努力阻止这种情况发生。给这个类一个它应得的名字;)。找出某个东西是接口还是类本身是抽象的是没有问题的。这已经在语言中了。因此,另外将其放入同一个类名中(这对于父类来说有点相同,但是您通常会自然地命名相关的子类)只是多余的。
最后你只需使用类的名称即可。因此,找到好的名字而不是技术名称可能是底线。本书中有一个很好的章节,名为“清洁代码”,介绍如何为类找到好的名称。
接下来,使用命名空间(如果由于 PHP 版本而无法使用它们,则使用前缀形式的伪命名空间)并将类放入子目录中,并使用一个文件 = 一个类。
经常重构并检查您的代码;)。快乐编码;)
It's hard to answer such a question because it depends a lot on what you feel good for yourself. From my own practice I can only suggest to give natural and not technical names. I nearly totally dropped to use Interface or Abstract in my classnames, dropped using a short version like
C
,I
orA
in front of the name. I even now try to prevent such. Give the class a name that it deserves ;).There is no problem to find out if something is an interface or a class itself is abstract. That's already in the language. So additionally putting that into the same classname (and that's a bit the same for parent classes, however you often naturally name child classes related) is just superfluous.
In the end you just use your classes by their name. So find good names instead of technical names is probably the bottom line. There is a nice chapter in the book called Clean Code about finding good names for your classes.
Next to that, make use of namespaces (or pseudeo-namspaces in form of prefixes if you can't use them due to the PHP version) and place the classes into subdirectories and use one file = one class.
Refactor often and review your code ;). Happy coding ;)
我认为最好的是 PSR-4。另外 PSR-0 已弃用。
在开始用 PHP 开发第一个严肃项目之前,我做了很多研究并意识到最佳实践是遵循通用标准 (PSR)。当然我们是很自由的。在某些情况下,我们可以为自己的项目弯曲一些零件。
每个 PHP 开发人员都必须查看 PSR。
I think best thing is PSR-4. Also PSR-0 deprecated.
Before I started developing my first serious project in PHP, I had done a lot of research and realized that the best practice was to follow the general standards (PSR). Of course we are quite free. In some cases, we can bend some parts for our own project.
Every PHP developer must look at PSR.
命名文件没有规则,除非您使用框架。您可以随意命名文件,但是,如果您使用框架,则在尝试加载文件时可能会导致错误。
最好的实现是您在使用时感觉最好的实现。
There are no rules for naming files, except when you use a framework. You can feel free for naming your files just as you want, however, if you are using a framework, it can cause an error when trying to load the files.
The best implementation is the one you feel best when using.