PHP coding standard

The Magento core development team uses the Magento Coding Standard. We recommend that developers who create Magento extensions and customizations also use this standard.

The Magento Coding Standard provides a set of rules that covers the following:

  • PSR-1 and PSR-2 compliance
  • The use of insecure functions
  • Unescaped output
  • The use of deprecated PHP functions
  • PHP code syntax
  • Naming convention
  • The use of PHP superglobals
  • Empty code blocks
  • Improper exception handling
  • Raw SQL queries and many other general PHP and Magento-specific code issues.

Coding standard compliance

Developers should consistently use PHP_CodeSniffer to enhance the readability of the code and ensure that it meets the Magento Coding Standard. PHP_CodeSniffer is the most popular tool in use throughout the PHP development community. It provides the mechanism of checking code compliance with specific coding standard.

The set of Magento rules is located in ruleset.xml file of Magento Coding Standard.

Learn more about using rule sets with PHP CodeSniffer ruleset

Literal Namespace Rule

For class name resolution, use the ::class keyword instead of a string literal for every class name reference outside of that class. This includes references to:

  • Fully qualified class name
  • Imported/non-imported class name
  • Namespace relative class name
  • Import relative class name

Examples:

1
  $this->get(ClassName::class);
1
  $this->get(\Magento\Path\To\Class::class);

The Coding standards overview introduces Magento-specific practices for PHP, JavaScript, and JQuery.