This is part of a series on Magento Best Practices. Click here to read more.
Coding standards are an agreed upon guideline that recommend a certain programming style. They adhere to an overall standard that maintains code quality. Simply put, it makes the code easier to manage. When developing your own modules, you should adhere to the Magento coding standards and practices adopted by Magento. Don’t go off into left field and do it your own way. It doesn’t make you unique. It just makes your code harder to maintain.
The principles you should follow are:
- Adhere to Zend Code Standards
- Comment Your Code
- Leverage Existing Libraries
- Use minimum Supported PHP version
Adhere to Zend Code Standards
Magento adopted the Zend Framework 1.x coding standards. This adheres to class naming conventions, file formatting, and coding style.
There are a few exceptions they made from this convention. They do not adhere to line length standards, (Zend uses 80 characters) or Abstract class naming conventions.
Magento 1.x doesn’t make use of namespacing, so you should not either. It is not supported via the autoloader.
Comment Your Code
When coding, it should be the adopted standard to also comment your classes and methods.
A caveat to this is putting in non-descript or obvious comments. You should always write your code to be easy to read. If you have to put in a comment that can be solved with a descriptive variable, consider refactoring the code.
Your methods should always have the appropriate docblocks. @param , @returns , and @throws should be automatically used. This makes it easier to know what your method needs and what it returns. Don’t make other developers guess.
Inline comments should only be used if they are necessary. Things such as deep method recursion or highly abstracted calls can use a quick comment to show what the intention is.
A good comment looks like:
// Parse filters and format them to be applicable for collection filtration
A bad comment looks like:
// loop through array and increment $i by 1
Leverage Existing Libraries
This gives a nod to the old adage, “don’t reinvent the wheel”. If you have to perform an action, (such as file read/writes, connecting to an external service, or any other typical programming problem), chances are that there is already an existing library that has solved that problem.
The entire Zend Framework is available inside of Magento. There is also a robust library called the Varien library also available to you. There is also a number of other libraries available for you. They are located in the lib/ directory.
- Need to parse json? Magento can do that.
- Need to connect via PEAR? Magento can do that.
- Need to manipulate files on the filesystem? Magento can do that.
- Need to validate form data? Magento can do that.
Not so bad. Want to go deeper? We can!
- Need convert measurements, such as cooking units, distance, frequency, power, pressure, or temperature? Magento can do that!
- Need Queuing functionality? Magento can do that!
- Need to parse Markup? Magento can do that!
- Need to auto-generate code or use Reflection? Magento can do that!
- Need to generate Barcodes? Magento can do that!
The point is, the moment you start to use low level functions in php, see if a library can get you there faster.
Use Minimum Supported PHP Version
When developing extensions, you should always check which Magento version you are supporting. Depending on the Magento version, the supported PHP version will change.
EE 1.14 and CE 1.9 both support PHP 5.4 and 5.5
Earlier versions support 5.2 – 5.3. You can update it to PHP 5.4 with a patch.
The last thing you want is to force your users to apply a patch, or make some sort of upgrade that might not be supported on their system. Just because you code in PHP 5.6 doesn’t mean your Magento users should be doing the same.