Php file naming

Is there a php file naming convention? Not to mention php class files, but files that mostly contain HTML with some php inside them. Should it my-file-example.php

or myFileExample.php

or myFileExample.php

?

+8


source to share


7 replies


There is no definitive naming convention in PHP and they can differ in structure. If you are not using a framework, you can use one of the popular ones:

  • with_underscores
  • PascalCased
  • camelCased

This table can be helpful and tell you more about naming (not only file names, but also class, method and function 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 that you agree to the naming convention.

+11


source


PSR-4 recommends using a directory hierarchy to represent the namespace.



For object-oriented programming, the autoloader

+2


source


There are really no conventions for PHP files.

You just have to keep the same in your project.

I recommend that you use camelCase .

+1


source


I suggest you use underscore separators my_file_example.php

, CodeIgniter for example uses this convention for viewing files.

With a dash, some IDEs / text editors do not select all filnames by double-clicking on them, but with an underscore select the entire file name in general.

0


source


There is no specific naming convention for core php files to use your own naming convention, but you can name it based on how your file works.

Like php file only html is displayed and then given my_file_view.php

if it has a database operation thenmy_file_db.php

etc. for a function and for other specifications you can use it.

0


source


Usually you need to accept 2 agreements:

  • used infrastructure
  • PSR-4

At best, these 2 are the same. I usually try to avoid cases where the environment is trying to force some custom rules.

In any case, the main thing is to remain constant throughout the entire project. Once you choose a convention to follow, stick to it until you start working on another project. There is nothing worse than different naming conventions in the same project, it doesn't matter if we're talking about files, classes, methods, etc. Be consistent.

0


source


You will come across packages with filenames like

class.phpmailer.php or
ImapMailbox.php

      

(both classes contain ClassName {} )

(Personally, I use camelCase for fnames, from var-habits I think.)

-4


source







All Articles