我应该为我的 PHP 类文件命名什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1209720/
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:33:07  来源:igfitidea点击:

What should I name my PHP class file?

phpclassnaming-conventions

提问by johnnietheblack

I have seen many different naming schemes and extensions used for PHP files that are basically just classes. Some examples:

我见过许多用于 PHP 文件的不同命名方案和扩展名,它们基本上只是类。一些例子:

  1. myClass.php
  2. myClass.class.php
  3. myClass.class
  1. 我的类.php
  2. myClass.class.php
  3. 我的类.class

What is the difference, and which is better?

有什么区别,哪个更好?

回答by Nate

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 file MyClass.php. And MyClass.class.phpworks too.

类名通常以大写字母开头(例如:)class MyClass。如果您这样做,那么您将把文件命名为MyClass.php. 也MyClass.class.php有效。

MyClass.classis a bad idea that could allow someone to view your source code if they request that file by name. Using the .phpextension 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.

MyClass.class这是一个坏主意,如果有人按名称请求该文件,则可能允许他们查看您的源代码。使用.php扩展确保用户只会在文件被 PHP 解释器处理后看到输出——对于只包含一个类的文件——是一个空白页面。

Finally, look into autoload(), which saves you the trouble of calling require_oncein order to load your class.

最后,查看autoload(),它可以节省您调用require_once以加载您的类的麻烦。



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 .phpextension—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.

更新:使用 PHP 编码标准PSR-4,现在有一种半官方的方式来命名您的类文件。之前的建议——将你的类文件命名为带有.php扩展名的类——仍然有效。但是现在您应该将该类文件放在以您的类命名空间命名的子目录中;PSR-4 要求您的所有类都包含在您定义的命名空间中。

What do you get for this? Autoloading for free! If you're using Composer, you can specify the top-level directory for your classesand Composer will autoload them. No more requirestatements to load your classes.

你能得到什么?免费自动加载!如果您使用Composer,您可以为您的类指定顶级目录,Composer 将自动加载它们。没有更多的require语句来加载你的类。

If you don't want to do this, you don't have to: PSR-4 is a recommendation, not a requirement.

如果您不想这样做,则不必这样做:PSR-4 是建议,而不是要求。

回答by chaos

There is no difference beyond what you see. The ones with .classare helpful if you use autoloadingand don't have a specific directory where your class files live. Otherwise, it's entirely arbitrary. (I used to use ClassName.class.php.)

除了你所看到的,没有什么区别。与个位.class,如果你使用是有帮助的自动加载,并没有在您的类文件住的特定目录。否则,这完全是任意的。(我曾经使用过ClassName.class.php。)

回答by Thomas Owens

I prefer MyClass.php - the exact same name (including capitals) as the class.

我更喜欢 MyClass.php - 与类完全相同的名称(包括大写字母)。

However, if you are working on an existing project, you should use whatever naming scheme the project has.

但是,如果您正在处理现有项目,则应使用该项目具有的任何命名方案。

回答by xentek

Unless working on an existing project or platform, I tend to lean towards a documented set of conventions, such as Zend's: http://framework.zend.com/manual/en/coding-standard.naming-conventions.html

除非在现有项目或平台上工作,否则我倾向于倾向于一组文档化的约定,例如 Zend:http: //framework.zend.com/manual/en/coding-standard.naming-conventions.html

Plus this means you can use tools like PHP Code Sniffer to ensure everyone sticks to the convention.

此外,这意味着您可以使用 PHP Code Sniffer 之类的工具来确保每个人都遵守约定。

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.

无论哪种方式,一致性都是游戏的名称。它使其他人(包括几个月后的您自己)更容易掌握代码库。