Zend framework coding style standard

Featured Image © by rodolfoclix/sxc

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:

< ?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:

< ?php
$string = 'My string.';
[/sourcecode]
 
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:
[sourcecode="php"]
//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!";
[/sourcecode]
 
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:
[sourcecode="php"]
//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.):

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.):

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:

/**
* @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.

Cheers!


12 comments

  1. Mado: yes it is possible using Zend_AMF, to comunicate with a PHP backend, using the entire Zend Framework or just the AMF component.

  2. Hi mladen, i have question about Zend i create chat application platform using adobe air and im using server from adobe (LCCS LveCycle) but i want to create server by zend to replace from adobe to zend that possible ?

  3. Check out some changes that will be made to Zend Framework coding standard at version 2.0

    http://zend-framework-dev.blogspot.com/2011/03/zend-framework-2-coding-standards-draft.html

    Additionally just want to note about spacing. The following spacing and brackets-placement rules are defined in Zend framework Coding Standard (PHP 5.2+):

    /**
     * MyClass
     * ......
     */
    class MyClass
    {
        public $var1 = 5;
    
        public function method1() {
            while (1 < 1000) {
                for ($i = 0; $i < 500;$i += 100) {
                    echo "Something";
                }
             }
        }
    
        public function method2() {
            while (1 < 1000) {
                for ($i = 500; $i > 0; $i -= 100) {
                    echo"$var1";
                }
            }
        }
    }
  4. @BOOMER – Thanks for the comment and especially thanks for the link. I wasn’t aware that it got the same… 😀

  5. @ Kamil – it’s not a “mistake”, but for better readability, its recommended to use one space as standard. For example:

    //Line 1:
    'take'.'a'."$look".'at'."$this ,".'and'."$this";
    //Line 2:
    'take' . 'a' . "$look" . 'at' . "$this ," . 'and' . "$this";

    Which one is more readable? I hope now you understand.

    Cheers!

  6. Mladen, what is the difference in concatenation with spaces or without? That was only one mistake from your post that I do all the time and I’m interested why?

  7. To both of you – thanks for kind words. I was hoping it would get useful to someone!

    Cheers!

  8. I already use most of these, and the documentation and classes indentation issues has arisen a few times but now that we are starting to standardise these things in the team it is really helping.

    I had also been using single quotes for strings (where applicable) rather than double quotes as it seem to be the preferred way within the team and I became accustomed to that, however I did not realise there would be an associated performance benefit but thinking about it now, it does make sense.

    Many Thanks.

    Chris.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.