mandriva
 

MDS Style Guide for PHP Code

Cédric Delfosse

Revision History
Revision $Revision: 58 $ $Date: 2007-10-04 18:26:31 +0200 (Thu, 04 Oct 2007) $ $Author: cedric $

Abstract

Coding conventions for the PHP code of all MDS components


1. Introduction

This document sets the coding conventions for the PHP code of all MDS components (like the MMC web interface for example).

Convention from http://pear.php.net/manual/en/standards.php apply too.

2. Code layout

Indentation: use 4 spaces per indentation level, no tabs allowed.

Encoding: the source code must always use the UTF-8 encoding.

For long html code, templates and views prefer using "short PHP tags":

<body>
    <h1><?= $title ?></h1>
    <ul>
    <? foreach ($arr as $value) { ?>
       <li><?= $value ?></li>
    <? } ?>
    </ul>
</body>

Code is more explicit and preserve html code source indentation.

3. Code indentation and organisation

block "for", "function", "switch", "if",etc... always end with opening braces on the same line.

if ($val == value) {
   echo $val;
} else {
   return -1;
}

foreach ($arrParam as $singleItem) {
    print $singleItem;
}

Function with long args (more than one line size)

myFunction($value1,
           $value2,
           $morevalue4,
           $val5);

4. Comments

They are written in english.

They always start with a capitalized first word.

There is always a space between the // and the begin of the comment. // and /* are fine. Don't use #.

All functions must have a correct doxygen header.

5. Naming conventions

ClassName : CapitalizedWords

functionName : mixedCase for all function name

_membersValue : member value of a class begin with a "_"