If you wish to participate in the development of XodaGallery, we need to follow some guidelines for the coding. This is because the code should feel consistent troughout the whole application, and therefore be more readable. Please note that this guidelines are formed for further development of XodaGallery, all files might not comply at this moment.
Save your files as UTF-8 encoded. 1)
End of line should be UNIX style. This is possible to set in the preferences of most editors. 2)
Wich editor to use is up to you, as long as the code follow theese guidelines.
Use starting brace at the same line as the command and closing brace at seperate line.
// braces prefered this way if ($life == '42') { // some code }
Use single-quotes whenever you can.
// avoid as much as possible $variable = "This is of some $variable2 to me."; // prefered $variable = 'This is of some ' . $variable2 . ' to me.';
Use tabs to indent the code. Set tabspaces to 4 in your editor. In first columns it's ok with one space, to make first level comments stand out.
// you can use one space here $days = array('monday','tuesday'); // inside loop use tab foreach ($days as $d) { // this is tabbed indented echo $d; // if you nest, use several tabs if ($d == 'monday') { echo 'What a day!'; } }
No hungarian notation in normal variables...
// wrong: $int_age = 32; // wrong: $iAge = 32; // right: $age = 32;
... BUT in globals like session-, form- and cookie-names:
// formdata uses "frm_" as hungarian notation $input = $_POST['frm_input']; // sessiondata uses "sess_" as hungarian notation $user = $_SESSION['sess_user_id']; // cookies uses "cook" as hungarian notation $remember = $_COOKIE['cook_memory'];
This is to avoid trouble on installations where the server uses register_globals.
Use underscore, not camelcase.
// wrong: $MyName = 'Mattias'; // right: $my_name = 'Mattias';
Use tags and practice from PHP Documentors manual (JavaDoc-style tags). Many of the tags have ”@” in them (see below).
The beginning of a PHP-file should always start with a commentblock stating
Example
/**
* @package XodaGallery
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @copyright Mattias Wirf 2008 <xodamattias@users.sourceforge.net>
* Optional explanation of what the file does
*/
For functions and classes comments, follow suggestions from PHPDoc. Always give input paramaters (arguments) and return-vale.
/** * Creates a session for user * @param string $username Name given by user in loginform * @param string $password Password given by user in loginform * @return bool True if valid user, false if not. */ function login($username, $password) { // some code }
Simply use the forward slashes
// user the short one hand forward-slash way $variable = somefunction();
Use shorthand CSS when you can. Some examples:
#someid { border: 1px solid #ccc; color: #000; padding: 0 0 10px 3px; margin: 0 auto; }
Make your code easy to find in css-documents, keep it in one place and a comment above:
/************ * Commentform ***********/ #commentform {}
Indent with tabs. First column can indent by a space, to seperate from commentlines. Example:
/************ * Commentform ***********/ #commentform fieldset { border: 1px solid #ccc; background: #f9f9f9; }