File: coding_standards

package info (click to toggle)
php-doc 20061001-1
  • links: PTS
  • area: non-free
  • in suites: etch, etch-m68k
  • size: 45,764 kB
  • ctags: 1,611
  • sloc: xml: 502,485; php: 7,645; cpp: 500; makefile: 297; perl: 161; sh: 141; awk: 28
file content (253 lines) | stat: -rw-r--r-- 5,786 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
GENERAL:

References:  http://pear.php.net/manual/en/standards.php
             http://www.php.net/manual/howto/

Examples need to be clear and simple, but should show the possibilities and
usage of the functions used.  Only use OOP features where you would like to
present them, use simple functions in other areas.

Example guidelines:

0. Requirements

   - Work with register_globals on or off
   - When appropriate, use superglobals
   - Never generate PHP errors (E_ALL friendly)
   - Be short and generic
   - Follow the PEAR coding standards

1. Program listing roles  (<programlisting role="xxx">)
   
   PHP examples should always have programlisting role="php"  Only PHP
   examples should have this role.  Possible roles are:
   
   - c           (C code) 
   - html        (100% XHTML)
   - php         (Some PHP)
   - shell       (commandline, bash, etc)
   - sql         (SQL statements)
   - apache-conf (Apache)

2. Titles

   When appropriate, it's encouraged to include the function name in
   the title, for example:
   
   <title>A <function>strlen</function> example</title>

3. Code placement

   The contents start at column/row 0 in the example.  For example, this
   means your example's content will be flush against the <![CDATA[ tag.

4. PHP tags

   Always use long php tags (<?php) and never short tags (<? or <?=).

5. CDATA

   Always use <![CDATA[ ... ]]> as this increases the readability of the
   examples.  For example, you literally write < instead of &lt; inside
   of CDATA.  Nothing in CDATA is parsed, it's taken literally.  So, you
   cannot use links, dev-comments, <function>, etc.

6. Deprecated code

   Do not use aliases or deprecated syntax.
   
7. Use of newer PHP features

   If an example uses features, such as arguments specific to a newer
   version of PHP, add a comment that mentions this.  For example:
   
   // Second argument was added in PHP 4.1.0
   foo('bar', 'baz');
   
   If appropriate, show examples that work in older versions of PHP but
   do not use reserved function names.  For example, a PHP 4.2.3 version
   of file_get_contents() should not be named file_get_contents().

8. Use of booleans in examples

   Do not use entities such as &true; in examples but instead write them
   out as TRUE, FALSE, and/or NULL.

9. Spacing

   Never use tabs, only use spaces.  Intention levels are four spaces 
   and do not intent the first level of code.  For example:
   
   Good:
   -------------------------
   <?php
   $str = 'Hello World';
   function foo($str)
   {
       return $str;
   }
   ?>
   
   Bad:
   -------------------------
   <?php
       $str = 'Hello World';
       function foo($str) 
       {
           return $str;
       }
   ?>

ERROR HANDLING

This section isn't yet complete but there are three main ways to
implement error handling in the PHP manual:

  a) Use of the 'or' operator.
  
     This is okay for development code but not ideal for production as use
     of 'or' is rather limiting.  An example use:
  
     foobar($lname) or die(...);
     
  b) A boolean check, along with {braces}
  
     This allows additional expressions inside the {braces} but requires
     more code.  This is the preferred method.  An example use:
     
     if (!foobar($lname)) {
         ...
	 exit;
     }
     
  d) trigger_error()
  
     There is debate on whether to use trigger_error() in the examples fo for
     now, do not use it (at least until the error handling docs are updated).

ABOUT VARIABLES/CONSTANTS/STRINGS:

1. Don't use variables which are not set in examples.

2. Constants should always be all-uppercase.

3. Use single quotes ' when appropriate.

4. For output use echo, instead of print.

5. Lowercase html-tags.

6. Variables in strings:

   Strings in strings

     This is of course debatable and subject to personal preference.  The
     two main methods are inline or concatenation:
   
     echo "bar is $bar";
     echo "bar is {$bar}";
     vs
     echo 'bar is ' . $bar;
   
     All of the above methods are acceptable.

   Arrays in strings
   
     As constants aren't looked for in strings, the following is fine
     but may confuse newbies so it's not to be used in examples:
     
     echo "an $array[key] key";
     
     Instead, consider these:
     
     echo "an {$array['key']} key";
     echo 'an ' . $array['key'] . ' key';


HOWTO WRITE...

A: CONTROL STRUCTURES

See PEAR coding standards

B: FUNCTIONS:

1. FUNCTION NAMING:

Procedural function names should be lowercase.  If multiple words are
needed in the function name, use a _.  Example: foo_function();

OOP function names should follow the PEAR Coding Standards which
would be fooFunction().

2. FUNCTION CALLS
3. FUNCTION DEFINITIONS

See PEAR coding standards

C: COMMENTS
D: EXAMPLE URLS/EMAIL

See PEAR coding standards

E: EXAMPLE PRINTOUTS

For very short example printouts, use C++ style comment (//) on the
line where the output occurs, or in the description above the line:

echo $var; // 32

For longer example printouts, there are a couple methods which are
acceptable.  Medium sized output may be inline with the example
itself through use of /* comments */, for example:

<?php
$arr = foo();
print_r($arr);

/* Outputs:

Array 
(
    [0] => 'bread'
    [1] => 'peanut butter'
    [2] => 'jam'
)
*/
?>

For longer example printouts, use the <screen> container in conjunction
with <![CDATA[...]]>

<para>
 <example>
  <title>A <function>foo</function> example</title>
  <programlisting role="php">
<![CDATA[
<?php
$arr = bar();
print_r($arr);
?>
]]>
  </programlisting>
  <para>
   The output is:
  </para>
  <screen>
<![CDATA[
Array
(
    [0] => 'a';
    [1] => 'b';
    [2] => 'c';
    ...
)
]]>
  </screen>
 </example>
</para>


COMPLETE EXAMPLE SKELETON

See the HOWTO