I strongly support the idea that 'good' code is 'well-formated' code as well.
What I want to stress here is that general appereance of the code is not less
important then it's content. So I decided that my first post in this section would
be about code conventions and standards I use during my sleepless nights of fighting
with PHP.
Few potints I want to make before going deep inside the code:
1. I always separate OO and Procedural code.
2. I don't use shell-like comments ('#')
3. I always use full start-end tags i.e. <?PHP ?>
4. Edit plus has cool option of substituding tabs by spaces and
I always use it.
General:
So here is my template for PHP file (I use Edit+ where it's possible to set default
appereance of your template files):
<?PHP /* * +------------------------------------------------------------------------------+ * Project Name * +------------------------------------------------------------------------------+ * Copyright Notice(s) * +------------------------------------------------------------------------------+ * Disclaimer Notice(s) * ex: This code is freely given to you and given "AS IS", SO if it damages * your computer, formats your HDs, or burns your house I am not the one to * blame. * Moreover, don't forget to include my copyright notices and name. * +------------------------------------------------------------------------------+ * Author(s): Victor Farazdagi (Kuulest) * +------------------------------------------------------------------------------+ */ error_reporting(E_ALL);//For debugging purposes /*code goes here*/ /* *tab-width=4 *indent=4 *width=90 */ ?>
Operators:
I have several basic rules on operator formating:
1. I always add spaces before and after assigning, logical
and comparision operators:
//assignment operators:
$a = 5;
$my_str .= "bla bla bla";
$i += 2;
//logical and comparision operators:
if (($name == 'Viktor') or ($name == 'Andrei') ) {
echo 'Record found!';
}
2. I also try to use (but almost always fail) the same logic forfor arithmetic operators:
//Looks better $a = (10 * $b) / 20; //then this: $a = (10*$b)/20;3. Sometime I find the following structure usefull (just to increase readability):
$var_name_1 = 5; $b = 7;
Control Structures:
Again rules I use:
1. I let one space after control structure and its brakets i.e if ()
2. I always use {} for indicating control structure body regardless of number of
statments control structure contains.
3. I intendate by one tab (actually four spaces) code statements inside the
control structure.
4. Not merely a rule but I often put additional line between different
(not nested) control structures.
5. I never use alternative syntax for control structures
(ex: if (cond):statements endif;)
if; if..else; if..elseif;
if ($a == $b) {
/*code goes here*/
}
if ($a == $b) {
/*code goes here*/
}else {
/*code goes here*/
}
if ($a == $b) {
/*code goes here*/
}elseif ($a == $c) {
/*code goes here*/
}
for; foreach; while;
I let additional space after ';' in 'for' cond. structure.
for ($i; $i < 10; $i++ ) {
/*code goes here*/
}
foreach ($arr as $k => $v ) {
/*code goes here*/
}
while ($i < 10) {
/*code goes here*/
}
I specially omitted do..while structure, simply because I don't use it much.
switch;
switch ($age) {
case 19:
echo 'You are in the army now!';
break;
case 100:
echo 'Incredible!!';
break;
default:
echo 'Nothing to say!';
}
Functions and Classes
Although there is nothing special about functions and classes with respect to
formating since they merely utilize the techniques discussed above, I want
to make several remarks:
1. It's a golden rule for me to documentate functions as well as possible.
2. Same thing about classes but even in more rigorous terms.
3. Look at {} in classes and functions below. I place them different from
same in control structures.
/** Function: void login($user_name = 'nobody', $user_pass = 'nobody')
* ----------------------------------------------------------------
* Purpose: Member function handling user login process
* Arguments: $user_name - string, user name
* $user_pass - string, user password
* Returns/Assigns: Sets session variable $logged to 1 if function
* call succeeds and 0 if otherwise.
*/
function login($user_name = 'nobody', $user_pass = 'nobody')
{
/*code goes here*/
}
Classes:
/**
* HTML Form elemetns generator for PHP4.
* Explanation: class purpose is to generate, strore and transfere btw pages
* form elements and their respective values.
* Supported HTML Form elements:
* text; password; radio; checkbox; image; file; hidden; button;
* submit; reset;
*/
class Form
{
/*code goes here*/
}
Not that for classes I also prepare distict documentation file covering topics like
following:
/**
* Classname: Form
* Description:Class for generationg HTML form elements on fly.
* Supports all form elements:
* 'text', 'password', 'checkbox', 'radio', 'textarea',
* 'button', 'submit', 'reset', 'select', 'hidden',
* 'image','file'.
* Version: 0.5
* Author: Victor Farazdagi(Kuulest).
*/
/**
*Instance variables
*/
classname - 'Form'
debug - switches on/off debugging mode(not really implemented)
/**
*Instance methods
*/
All user-methods present in class.
/**
*Internal methods
*/
All system-methods (internal) present in class.
/**
*TODO List:
*/
That is all!
Now, I believe that it would be cool experience to know about your coding style.
Yours Kuulest
P.S. Sorry if text is too long, I worked hard to cut as many as posible..













