PHP: why the Zend coding style

In my PHP projects I strictly follow the Zend coding standard style. This is not because I want to jump on another web bandwagon, but simply because this coding style provides the best thing that a web developer can get from his/her code: consistency. There's nothing worse than a PHP code that first follows a coding convention and then switches to another one just in the middle of a component or routine. But there's even something worse than this bad habit: certain frameworks or CMS use a coding convention that makes life harder for a developer.

Take Wordpress for example. Every single core function (not to say methods), is written in lowercase characters. So if you have to use PHP's core functions within your Wordpress code, you lose a basic visual clue that allows you to keep the two types of functions separated. The point is that the overwhelming majority of Wordpress plugins and widgets follow this convention, because Wordpress wants you to use only its functions and its coding style. Eventually you end up with writing more in Wordpress than PHP, and this is exactly the same thing that happens in JavaScript with jQuery.

Zend is completely different. You write your classes, routines and class members in PHP. You're also free to choose between the Zend components and your own components. But remember: you still write in PHP, not in Zend. Zend is not addictive: if you want to use it to its full potential, you must know PHP first, then Zend. So if you know the basic concepts behind classes, interfaces and design patterns, you can use them either with Zend components or only with some specific component.

Why, for example, when you declare a function or method you have to put the opening brace on a separate line? Is is a fad? No. It's just because of the visual clue that you have when you take a look at your code. First comes the name of the function or method and then the opening brace:

function doSomething()
{

  // function body

}

The same visual principle applies to string concatenation. Between each . operator there must be a space. Why? Because by doing so you actually increase the legibility of your code:

$test = 'a' . 'b' . 'c';

Instead, the following is not easily legible, especially when you have multiple strings:

$test = 'a'.'b'.'c';

Anyway, this only my own personal experience. The important thing is to choose a coding style and always follow it consistently. wink

This entry was posted in by Gabriele Romanato. Bookmark the permalink.

Comments are closed.