我应该为我的 PHP 类文件命名什么?
我见过许多不同的 PHP 文件命名方案和扩展名,它们基本上只是类。 一些例子:
- myClass.php
- myClass.class.php
- myClass.class
有什么区别,哪个更好?
I have seen many different naming schemes and extensions used for PHP files that are basically just classes. Some examples:
- myClass.php
- myClass.class.php
- myClass.class
What is the difference, and which is better?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你们的编码标准是什么?
类名通常以大写字母开头(例如:
class MyClass
)。 如果您这样做,那么您将将该文件命名为MyClass.php
。MyClass.class.php
也可以工作。MyClass.class
是一个坏主意,如果有人按名称请求该文件,可能会允许他们查看您的源代码。 使用.php
扩展可确保用户仅在 PHP 解释器处理文件后才能看到输出,对于只包含类的文件来说,输出是一个空白页。最后,查看
autoload()
,这样就省去了调用require_once
来加载类的麻烦。更新:采用 PHP 编码标准 PSR-4 ,现在有一种半官方的方式来命名类文件。 之前的建议(将类文件命名为与带有
.php
扩展名的类相同的名称)仍然有效。 但现在您需要将该类文件放置在以您的类名称空间命名的子目录中; PSR-4 要求所有类都包含在您定义的命名空间中。你为此得到什么? 免费自动加载! 如果您使用 Composer,您可以 指定类的顶级目录,Composer 将自动加载它们。 不再需要
require
语句来加载您的类。如果您不想这样做,则不必这样做:PSR-4 是建议,而不是要求。
What are your coding standards?
It’s common to start class names with a capital letter (for example:
class MyClass
). If you do this, then it follows that you would name the fileMyClass.php
. AndMyClass.class.php
works too.MyClass.class
is a bad idea that could allow someone to view your source code if they request that file by name. Using the.php
extension ensures that the the user will only see the output after the file is processed by the PHP interpreter which—for a file that contains nothing but a class—is a blank page.Finally, look into
autoload()
, which saves you the trouble of callingrequire_once
in order to load your class.Update: With PHP coding standard PSR-4, there is now a semi-official way to name your class files. The previous advice—to name your class file the same as the class with the
.php
extension—still stands. But now you are expected to place that class file in a subdirectory named after your class namespace; PSR-4 requires that all of your classes be contained in a namespace defined by you.What do you get for this? Autoloading for free! If you’re using Composer, you can specify the top-level directory for your classes and Composer will autoload them. No more
require
statements to load your classes.If you don’t want to do this, you don’t have to: PSR-4 is a recommendation, not a requirement.
除了您所看到的之外,没有任何区别。 如果您使用 ,带有
.class
的内容会很有帮助自动加载并且没有类文件所在的特定目录。 否则,这完全是任意的。 (我曾经使用ClassName.class.php
。)There is no difference beyond what you see. The ones with
.class
are helpful if you use autoloading and don't have a specific directory where your class files live. Otherwise, it's entirely arbitrary. (I used to useClassName.class.php
.)我更喜欢 MyClass.php - 与类的名称完全相同(包括大写字母)。
但是,如果您正在处理现有项目,则应该使用该项目具有的任何命名方案。
I prefer MyClass.php - the exact same name (including capitals) as the class.
However, if you are working on an existing project, you should use whatever naming scheme the project has.
除非在现有项目或平台上工作,否则我倾向于倾向于一组记录在案的约定,例如 Zend 的: https://framework.zend.com/manual/1.12/en/coding-standard.naming-conventions.html
另外,这意味着您可以使用 PHP 等工具代码嗅探器确保每个人都遵守约定。
无论哪种方式,一致性都是游戏的名称。 它使其他人(包括几个月后的您自己)能够更轻松地掌握代码库的速度。
Unless working on an existing project or platform, I tend to lean towards a documented set of conventions, such as Zend's: https://framework.zend.com/manual/1.12/en/coding-standard.naming-conventions.html
Plus this means you can use tools like PHP Code Sniffer to ensure everyone sticks to the convention.
Either way, Consistency is the name of the game. It makes it a lot easier for others (including yourself months from now) to get up to speed on a code base.