Magento themes and extensions

Zend framework coding style standard

Share

Hello again! Today I’ll discuss some of the basic coding rules if your’re using Zend framework (or even Magento, which is written over Zend framework). Some might think that this is an redundant topic, but I strongly disagree – if you work in a team.

Latin introduction

Well, the story about style standards is quite like goes pretty much like old Latin saying “De gustibus non est disputandum” which means “There is no disputing about tastes”. But same things apply to it as well, if you have a big corporate meeting, you won’t go dressed in diving suit, but in a regular one. If you take an parallel look at the coding styles, its pretty much the same. If you’re going to work with a team on new Zend project, some things must be applied. Otherwise you’ll end up in quite a mess.

The main stuff

There are couple of things that matter the most, and I’ll only cover those, according to Zend framework official documentation.

PHP open / close tags

Well, regarding Zend framework official documentation, as well as some of my personal experience, there’s one main rule:

1
2
3
< ?php
//Always and I do mean always, use full php tags, not the short ones
?>

Second rule is (from my experience), although I wrote it in example above, omit the closing tag, and make sure that opening tag is starting from character one of the file. I’m saying this because it wont break the compiling, nor will it cause any errors / warnings. And by this, you’re making sure that in your included files (controllers / models) you don’t have any blank characters which would cause problems with headers most likely.

Of course, if you’re in phtml file, you must use closing tag as well.

Working with strings

When working with string literals, like this one:

1
2
< ?php
$string = 'My string.';

Try to make a habit of using single quotes around the string. This one is because when you use double quotes PHP will search for variable replacement inside a string, opposed to single quotes. Obviously, single quotes are faster when working with strings.

If you’re using much of single quotes as literals (SQL for example), or if you have a variable/s inside it, you should use double quotes, like in following example:

1
2
3
4
5
//As for single quotes, something like in this simple example:
$sql = "SELECT * FROM table WHERE name='John', date='2010-12-29'";
//And as far for variables, you can use either of these:
$greeting = "Hello $name, welcome back!";
$greeting = "Hello {$name}, welcome back!";

Of course, this is one that doesn’t have to be followed to the letter, but you should make a practice of it.

And another important thing to mention here is concatenation. In PHP it’s achieved by “.” sign. Like this:

1
2
3
4
5
//This is the right way, with spacing
$string = 'First part' . ',and second one.';
//And this is the wrong way:
$string = 'First part'.',and second one.';
//>

A little bit about Classes

First, take a look at this (Example 1.):

1
2
3
4
5
6
7
8
9
10
11
12
class MyClass{
function method1(){
while(1 < 1000){
for($i=0;$i < 500;$i+=100){
echo"Something";
}}}
$var1=5;
function method2(){
while(1 < 1000){
for($i=500;$i>0;$i-=100){
echo"$var1";
}}}}

And the, take a look at this (Example 2.):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class MyClass
{
public $var1 = 5;
function method1()
{
while(1 < 1000)
{
for($i = 0; $i < 500; $i += 100)
{
echo "Something";
}
}
}
function method2()
{
while(1 < 1000)
{
for($i = 500; $i > 0; $i -= 100)
{
echo "$var1";
}
}
}
}

Now, you tell me which one is more readable? “Example 1″ or “Example 2″? Joking a side, a couple of rules are applied here, as follows:

  1. Don’t mess with your indents, or the’ll mess with you
  2. Put out a proper spacing in method parameters, logic conditions, etc. It really improves the readability.
  3. Variables with visibility set declared at the beginning of the class (by default its set to public, but that’s just PHP’s compatability, not the right way)

And I must mention documentation as well here

Well, personally I didn’t like overkill on documentation, but on the other hand, it became quite a help in Zend framework. So I changed my mind. I’ll say that you only need 6 things by order to apply to rules, and here they are:

1
2
3
4
5
/**
* @category   Zend
* @package    Zend_Magic
* @subpackage Wand
*/

Goodbye

And that’s pretty much it. If you follow these rules, or rather guidelines, you’ll be more productive both as an individual, and as a team.

Source: http://inchoo.net/zend/zend-framework-coding-style-standard/

Leave a Reply

You must be logged in to post a comment.