group | subgroup | title | landing-page | menu_title | menu_order | functional_areas | |
---|---|---|---|---|---|---|---|
coding-standards |
01_Coding standards |
PHP coding standard |
Coding standards |
PHP coding standard |
2 |
|
The Magento core development team uses the Magento Coding Standard{:target="_blank"}. 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{:target="_blank"} and PSR-2{:target="_blank"} 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.
Developers should consistently use PHP_CodeSniffer{:target="_blank"} to enhance the readability of the code and ensure that it meets the Magento Coding Standard. PHP_CodeSniffer{:target="_blank"} 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
][ruleset.xml]{:target="_blank"} file of Magento Coding Standard.
Learn more about using rule sets with PHP CodeSniffer [ruleset]{:target="_blank"}
For class name resolution, use the ::class
{:target="_blank"} 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:
$this->get(ClassName::class);
$this->get(\Magento\Path\To\Class::class);
The [Coding standards overview]{:target="_blank"} introduces Magento-specific practices for PHP, JavaScript, and JQuery.
[Coding standards overview]: {{ page.baseurl }}/coding-standards/bk-coding-standards.html [ruleset]: http://pear.php.net/manual/en/package.php.php-codesniffer.annotated-ruleset.php [ruleset.xml]: https://github.com/magento/magento-coding-standard/blob/develop/Magento2/ruleset.xml [Magento Coding Standard]: https://github.com/magento/magento-coding-standard