PHP coding standard
The core development team uses the Magento Coding Standard. We recommend that developers who create 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 Adobe Commerce and Magento Open Source-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 rules is located in ruleset.xml
file of the 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:
Copied to your clipboard$this->get(ClassName::class);
Copied to your clipboard$this->get(\Magento\Path\To\Class::class);
The coding standards overview introduces Adobe Commerce and Magento Open Source-specific practices for PHP, JavaScript, and JQuery.