Naming php files

2020-05-26 12:03发布

Is there any convention for naming php files? Not talking about php class files, but files that are mostly HTML with some php inside them. Should it be my-file-example.php or myFileExample.php or myfileexample.php?

标签: php html
7条回答
Juvenile、少年°
2楼-- · 2020-05-26 12:16

There is no specific for the core php files you have to use your own naming convention, but you can name on the bases of you file work.

Like php file is only display the html then give my_file_view.php if it has database operation then my_file_db.php

and so on for the function and for other specification you can use it.

查看更多
beautiful°
3楼-- · 2020-05-26 12:17

You'll come across packages using filenames as

class.phpmailer.php or
ImapMailbox.php

(both containing classes with ClassName {})

(Personally, I use camelCase heavily for fnames, from var-habits i guess. )

查看更多
成全新的幸福
4楼-- · 2020-05-26 12:24

I would suggest you to use underscore separators my_file_example.php, CodeIgniter for example use this convention for it's view files.

With dashes, some IDE/Text Editor don't select all the filname by double clicking on it, while with underscore it select, generally, all the file name.

查看更多
倾城 Initia
5楼-- · 2020-05-26 12:27

There is no definitive naming convention in PHP, and they can be differ by framework. If you don't use any framework u can use one of popular:

  1. with_underscores
  2. PascalCased
  3. camelCased

This table can be useful and tell you something more about naming convention (not only files but classes, methods and functions names):

╔═══════════════════════╦═════════════╦════════════╦══════════════╦════════════╦════════════╗
║      PHP Project      ║   Classes   ║  Methods   ║  Properties  ║ Functions  ║ Variables  ║
╠═══════════════════════╬═════════════╬════════════╬══════════════╬════════════╬════════════╣
║ Akelos Framework      ║ PascalCase  ║ camelCase  ║ camelCase    ║ lower_case ║ lower_case ║
║ CakePHP Framework     ║ PascalCase  ║ camelCase  ║ camelCase    ║ camelCase  ║ camelCase  ║
║ CodeIgniter Framework ║ Proper_Case ║ lower_case ║ lower_case   ║ lower_case ║ lower_case ║
║ Concrete5 CMS         ║ PascalCase  ║ camelCase  ║ camelCase    ║ lower_case ║ lower_case ║
║ Doctrine ORM          ║ PascalCase  ║ camelCase  ║ camelCase    ║ camelCase  ║ camelCase  ║
║ Drupal CMS            ║ PascalCase  ║ camelCase  ║ camelCase    ║ lower_case ║ lower_case ║
║ Joomla CMS            ║ PascalCase  ║ camelCase  ║ camelCase    ║ camelCase  ║ camelCase  ║
║ modx CMS              ║ PascalCase  ║ camelCase  ║ camelCase    ║ camelCase  ║ lower_case ║
║ Pear Framework        ║ PascalCase  ║ camelCase  ║ camelCase    ║            ║            ║
║ Prado Framework       ║ PascalCase  ║ camelCase  ║ Pascal/camel ║            ║ lower_case ║
║ SimplePie RSS         ║ PascalCase  ║ lower_case ║ lower_case   ║ lower_case ║ lower_case ║
║ Symfony Framework     ║ PascalCase  ║ camelCase  ║ camelCase    ║ camelCase  ║ camelCase  ║
║ WordPress CMS         ║             ║            ║              ║ lower_case ║ lower_case ║
║ Zend Framework        ║ PascalCase  ║ camelCase  ║ camelCase    ║ camelCase  ║ camelCase  ║
╚═══════════════════════╩═════════════╩════════════╩══════════════╩════════════╩════════════╝

Remember to being consistent with the naming convention.

查看更多
▲ chillily
6楼-- · 2020-05-26 12:35

There are usually 2 conventions you need to take into consideration:

  1. used framework
  2. PSR-4

In best scenario these 2 are the same. I usually try to avoid cases when a framework tries to force some custom rules.

Either way, the MOST important is to stay consistent throughout the project. Once you pick a convention to follow, stick with it until you start working on a different project. There's nothing worse than different naming conventions in a single project, doesn't matter if we're talking about files, classes, methods or whatever. Be consistent.

查看更多
在下西门庆
7楼-- · 2020-05-26 12:36

There is no really convention for PHP files.

You just should keep the same in your project.

I recommand you to use camelCase.

查看更多
登录 后发表回答