Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 2 | <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> |
| 3 | <head> |
| 4 | |
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
| 6 | <title>Style Guide : CodeIgniter User Guide</title> |
| 7 | |
| 8 | <style type='text/css' media='all'>@import url('../userguide.css');</style> |
| 9 | <link rel='stylesheet' type='text/css' media='all' href='../userguide.css' /> |
| 10 | |
| 11 | <style type="text/css" media="screen"> |
| 12 | code { |
| 13 | white-space: pre; |
| 14 | } |
| 15 | </style> |
| 16 | |
| 17 | <script type="text/javascript" src="../nav/nav.js"></script> |
| 18 | <script type="text/javascript" src="../nav/prototype.lite.js"></script> |
| 19 | <script type="text/javascript" src="../nav/moo.fx.js"></script> |
| 20 | <script type="text/javascript" src="../nav/user_guide_menu.js"></script> |
| 21 | |
| 22 | <meta http-equiv='expires' content='-1' /> |
| 23 | <meta http-equiv= 'pragma' content='no-cache' /> |
| 24 | <meta name='robots' content='all' /> |
| 25 | <meta name='author' content='ExpressionEngine Dev Team' /> |
| 26 | <meta name='description' content='CodeIgniter User Guide' /> |
| 27 | |
| 28 | </head> |
| 29 | <body> |
| 30 | |
| 31 | <!-- START NAVIGATION --> |
| 32 | <div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div> |
| 33 | <div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div> |
| 34 | <div id="masthead"> |
| 35 | <table cellpadding="0" cellspacing="0" border="0" style="width:100%"> |
| 36 | <tr> |
Derek Jones | 8917af7 | 2010-03-05 12:41:45 -0600 | [diff] [blame] | 37 | <td><h1>CodeIgniter User Guide Version 2.0.0</h1></td> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 38 | <td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td> |
| 39 | </tr> |
| 40 | </table> |
| 41 | </div> |
| 42 | <!-- END NAVIGATION --> |
| 43 | |
| 44 | |
| 45 | <!-- START BREADCRUMB --> |
| 46 | <table cellpadding="0" cellspacing="0" border="0" style="width:100%"> |
| 47 | <tr> |
| 48 | <td id="breadcrumb"> |
| 49 | <a href="http://codeigniter.com/">CodeIgniter Home</a> › |
| 50 | <a href="../index.html">User Guide Home</a> › |
| 51 | Style Guide |
| 52 | </td> |
| 53 | <td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.com/user_guide/" />Search User Guide <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" /> <input type="submit" class="submit" name="sa" value="Go" /></form></td> |
| 54 | </tr> |
| 55 | </table> |
| 56 | <!-- END BREADCRUMB --> |
| 57 | |
| 58 | <br clear="all" /> |
| 59 | |
| 60 | |
| 61 | <!-- START CONTENT --> |
| 62 | <div id="content"> |
| 63 | |
| 64 | |
| 65 | <h1>General Style and Syntax</h1> |
| 66 | |
Derek Allard | f3c4f27 | 2009-09-22 11:53:34 +0000 | [diff] [blame] | 67 | <p>The following page describes the use of coding rules adhered to when developing CodeIgniter.</p> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 68 | |
| 69 | |
| 70 | <h2>Table of Contents</h2> |
| 71 | <ul class="minitoc"> |
Derek Jones | 9babd66 | 2008-11-14 02:09:00 +0000 | [diff] [blame] | 72 | <li><a href="#file_format">File Format</a></li> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 73 | <li><a href="#php_closing_tag">PHP Closing Tag</a></li> |
| 74 | <li><a href="#class_and_method_naming">Class and Method Naming</a></li> |
| 75 | <li><a href="#variable_names">Variable Names</a></li> |
| 76 | <li><a href="#commenting">Commenting</a></li> |
| 77 | <li><a href="#constants">Constants</a></li> |
| 78 | <li><a href="#true_false_and_null">TRUE, FALSE, and NULL</a></li> |
| 79 | <li><a href="#logical_operators">Logical Operators</a></li> |
| 80 | <li><a href="#comparing_return_values_and_typecasting">Comparing Return Values and Typecasting</a></li> |
| 81 | <li><a href="#debugging_code">Debugging Code</a></li> |
| 82 | <li><a href="#whitespace_in_files">Whitespace in Files</a></li> |
| 83 | <li><a href="#compatibility">Compatibility</a></li> |
| 84 | <li><a href="#class_and_file_names_using_common_words">Class and File Names using Common Words</a></li> |
| 85 | <li><a href="#database_table_names">Database Table Names</a></li> |
| 86 | <li><a href="#one_file_per_class">One File per Class</a></li> |
| 87 | <li><a href="#whitespace">Whitespace</a></li> |
| 88 | <li><a href="#line_breaks">Line Breaks</a></li> |
| 89 | <li><a href="#code_indenting">Code Indenting</a></li> |
| 90 | <li><a href="#bracket_spacing">Bracket and Parenthetic Spacing</li> |
Derek Allard | 2f304d6 | 2010-07-05 07:40:26 -0400 | [diff] [blame] | 91 | <li><a href="#localized_text">Localized Text</a></li> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 92 | <li><a href="#private_methods_and_variables">Private Methods and Variables</a></li> |
| 93 | <li><a href="#php_errors">PHP Errors</a></li> |
| 94 | <li><a href="#short_open_tags">Short Open Tags</a></li> |
| 95 | <li><a href="#one_statement_per_line">One Statement Per Line</a></li> |
| 96 | <li><a href="#strings">Strings</a></li> |
| 97 | <li><a href="#sql_queries">SQL Queries</a></li> |
| 98 | <li><a href="#default_function_arguments">Default Function Arguments</a></li> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 99 | </ul> |
| 100 | |
Derek Jones | 9babd66 | 2008-11-14 02:09:00 +0000 | [diff] [blame] | 101 | <li> |
| 102 | |
| 103 | <h2><a name="file_format"></a>File Format</h2> |
| 104 | <div class="guidelineDetails"> |
| 105 | <p>Files should be saved with Unicode (UTF-8) encoding. The <abbr title="Byte Order Mark">BOM</abbr> |
| 106 | should <em>not</em> be used. Unlike UTF-16 and UTF-32, there's no byte order to indicate in |
| 107 | a UTF-8 encoded file, and the <abbr title="Byte Order Mark">BOM</abbr> can have a negative side effect in PHP of sending output, |
| 108 | preventing the application from being able to set its own headers. Unix line endings should |
| 109 | be used (LF).</p> |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 110 | |
Derek Jones | 9babd66 | 2008-11-14 02:09:00 +0000 | [diff] [blame] | 111 | <p>Here is how to apply these settings in some of the more common text editors. Instructions for your |
| 112 | text editor may vary; check your text editor's documentation.</p> |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 113 | |
Derek Jones | 9babd66 | 2008-11-14 02:09:00 +0000 | [diff] [blame] | 114 | <h5>TextMate</h5> |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 115 | |
Derek Jones | 9babd66 | 2008-11-14 02:09:00 +0000 | [diff] [blame] | 116 | <ol> |
| 117 | <li>Open the Application Preferences</li> |
| 118 | <li>Click Advanced, and then the "Saving" tab</li> |
| 119 | <li>In "File Encoding", select "UTF-8 (recommended)"</li> |
| 120 | <li>In "Line Endings", select "LF (recommended)"</li> |
| 121 | <li><em>Optional:</em> Check "Use for existing files as well" if you wish to modify the line |
| 122 | endings of files you open to your new preference.</li> |
| 123 | </ol> |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 124 | |
Derek Jones | 9babd66 | 2008-11-14 02:09:00 +0000 | [diff] [blame] | 125 | <h5>BBEdit</h5> |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 126 | |
Derek Jones | 9babd66 | 2008-11-14 02:09:00 +0000 | [diff] [blame] | 127 | <ol> |
| 128 | <li>Open the Application Preferences</li> |
| 129 | <li>Select "Text Encodings" on the left.</li> |
| 130 | <li>In "Default text encoding for new documents", select "Unicode (UTF-8, no BOM)"</li> |
| 131 | <li><em>Optional:</em> In "If file's encoding can't be guessed, use", select |
| 132 | "Unicode (UTF-8, no BOM)"</li> |
| 133 | <li>Select "Text Files" on the left.</li> |
| 134 | <li>In "Default line breaks", select "Mac OS X and Unix (LF)"</li> |
| 135 | </ol> |
| 136 | </div> |
| 137 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 138 | <h2><a name="php_closing_tag"></a>PHP Closing Tag</h2> |
| 139 | <div class="guidelineDetails"> |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 140 | <p>The PHP closing tag on a PHP document <strong>?></strong> is optional to the PHP parser. However, if used, any whitespace following the closing tag, whether introduced |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 141 | by the developer, user, or an FTP application, can cause unwanted output, PHP errors, or if the latter are suppressed, blank pages. For this reason, all PHP files should |
| 142 | <strong>OMIT</strong> the closing PHP tag, and instead use a comment block to mark the end of file and it's location relative to the application root. |
| 143 | This allows you to still identify a file as being complete and not truncated.</p> |
| 144 | <code><strong>INCORRECT</strong>: |
| 145 | <?php |
| 146 | |
| 147 | echo "Here's my code!"; |
| 148 | |
| 149 | ?> |
| 150 | |
| 151 | <strong>CORRECT</strong>: |
| 152 | <?php |
| 153 | |
| 154 | echo "Here's my code!"; |
| 155 | |
| 156 | /* End of file myfile.php */ |
| 157 | /* Location: ./system/modules/mymodule/myfile.php */ |
| 158 | </code> |
| 159 | </div> |
| 160 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 161 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 162 | <h2><a name="class_and_method_naming"></a>Class and Method Naming</h2> |
| 163 | <div class="guidelineDetails"> |
| 164 | <p>Class names should always have their first letter uppercase, and the constructor method should match identically. Multiple words should be separated with an underscore, and not CamelCased. All other class methods should be entirely lowercased and named to clearly indicate their function, preferably including a verb. Try to avoid overly long and verbose names.</p> |
| 165 | |
| 166 | <code><strong>INCORRECT</strong>: |
| 167 | class superclass |
| 168 | class SuperClass |
| 169 | |
| 170 | <strong>CORRECT</strong>: |
| 171 | class Super_class</code> |
| 172 | |
| 173 | <p>Notice that the Class and constructor methods are identically named and cased:</p> |
| 174 | |
| 175 | <code>class Super_class { |
| 176 | |
| 177 | function Super_class() |
| 178 | { |
| 179 | |
| 180 | } |
| 181 | }</code> |
| 182 | |
| 183 | <p>Examples of improper and proper method naming:</p> |
| 184 | |
| 185 | <code><strong>INCORRECT</strong>: |
| 186 | function fileproperties() // not descriptive and needs underscore separator |
| 187 | function fileProperties() // not descriptive and uses CamelCase |
| 188 | function getfileproperties() // Better! But still missing underscore separator |
| 189 | function getFileProperties() // uses CamelCase |
| 190 | function get_the_file_properties_from_the_file() // wordy |
| 191 | |
| 192 | <strong>CORRECT</strong>: |
| 193 | function get_file_properties() // descriptive, underscore separator, and all lowercase letters</code> |
| 194 | |
| 195 | </div> |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 196 | |
| 197 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 198 | <h2><a name="variable_names"></a>Variable Names</h2> |
| 199 | <div class="guidelineDetails"> |
| 200 | <p>The guidelines for variable naming is very similar to that used for class methods. Namely, variables should contain only lowercase letters, use underscore separators, and be reasonably named to indicate their purpose and contents. Very short, non-word variables should only be used as iterators in for() loops.</p> |
| 201 | <code><strong>INCORRECT</strong>: |
| 202 | $j = 'foo'; // single letter variables should only be used in for() loops |
| 203 | $Str // contains uppercase letters |
| 204 | $bufferedText // uses CamelCasing, and could be shortened without losing semantic meaning |
| 205 | $groupid // multiple words, needs underscore separator |
| 206 | $name_of_last_city_used // too long |
| 207 | |
| 208 | <strong>CORRECT</strong>: |
| 209 | for ($j = 0; $j < 10; $j++) |
| 210 | $str |
| 211 | $buffer |
| 212 | $group_id |
| 213 | $last_city |
| 214 | </code> |
| 215 | </div> |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 216 | |
| 217 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 218 | <h2><a name="commenting"></a>Commenting</h2> |
| 219 | <div class="guidelineDetails"> |
| 220 | <p>In general, code should be commented prolifically. It not only helps describe the flow and intent of the code for less experienced programmers, but can prove invaluable when returning to your own code months down the line. There is not a required format for comments, but the following are recommended.</p> |
| 221 | |
| 222 | <p><a href="http://manual.phpdoc.org/HTMLSmartyConverter/HandS/phpDocumentor/tutorial_phpDocumentor.howto.pkg.html#basics.docblock">DocBlock</a> style comments preceding class and method declarations so they can be picked up by IDEs:</p> |
| 223 | |
| 224 | <code>/** |
| 225 | * Super Class |
| 226 | * |
| 227 | * @package Package Name |
| 228 | * @subpackage Subpackage |
| 229 | * @category Category |
| 230 | * @author Author Name |
| 231 | * @link http://example.com |
| 232 | */ |
| 233 | class Super_class {</code> |
| 234 | |
| 235 | <code>/** |
| 236 | * Encodes string for use in XML |
| 237 | * |
| 238 | * @access public |
| 239 | * @param string |
| 240 | * @return string |
| 241 | */ |
| 242 | function xml_encode($str)</code> |
| 243 | |
| 244 | <p>Use single line comments within code, leaving a blank line between large comment blocks and code.</p> |
| 245 | |
| 246 | <code>// break up the string by newlines |
| 247 | $parts = explode("\n", $str); |
| 248 | |
| 249 | // A longer comment that needs to give greater detail on what is |
| 250 | // occurring and why can use multiple single-line comments. Try to |
| 251 | // keep the width reasonable, around 70 characters is the easiest to |
| 252 | // read. Don't hesitate to link to permanent external resources |
| 253 | // that may provide greater detail: |
| 254 | // |
| 255 | // http://example.com/information_about_something/in_particular/ |
| 256 | |
| 257 | $parts = $this->foo($parts); |
| 258 | </code> |
| 259 | </div> |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 260 | |
| 261 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 262 | <h2><a name="constants"></a>Constants</h2> |
| 263 | <div class="guidelineDetails"> |
Derek Allard | 5a8a455 | 2009-08-17 18:06:30 +0000 | [diff] [blame] | 264 | <p>Constants follow the same guidelines as do variables, except constants should always be fully uppercase. <em>Always use CodeIgniter constants when appropriate, i.e. SLASH, LD, RD, PATH_CACHE, etc.</em></p> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 265 | <code><strong>INCORRECT</strong>: |
| 266 | myConstant // missing underscore separator and not fully uppercase |
| 267 | N // no single-letter constants |
| 268 | S_C_VER // not descriptive |
| 269 | $str = str_replace('{foo}', 'bar', $str); // should use LD and RD constants |
| 270 | |
| 271 | <strong>CORRECT</strong>: |
| 272 | MY_CONSTANT |
| 273 | NEWLINE |
| 274 | SUPER_CLASS_VERSION |
| 275 | $str = str_replace(LD.'foo'.RD, 'bar', $str); |
| 276 | </code> |
| 277 | </div> |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 278 | |
| 279 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 280 | <h2><a name="true_false_and_null"></a>TRUE, FALSE, and NULL</h2> |
| 281 | <div class="guidelineDetails"> |
| 282 | <p><strong>TRUE</strong>, <strong>FALSE</strong>, and <strong>NULL</strong> keywords should always be fully uppercase.</p> |
| 283 | <code><strong>INCORRECT</strong>: |
| 284 | if ($foo == true) |
| 285 | $bar = false; |
| 286 | function foo($bar = null) |
| 287 | |
| 288 | <strong>CORRECT</strong>: |
| 289 | if ($foo == TRUE) |
| 290 | $bar = FALSE; |
| 291 | function foo($bar = NULL)</code> |
| 292 | </div> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 293 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 294 | |
| 295 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 296 | <h2><a name="logical_operators"></a>Logical Operators</h2> |
| 297 | <div class="guidelineDetails"> |
| 298 | <p>Use of <strong>||</strong> is discouraged as its clarity on some output devices is low (looking like the number 11 for instance). |
| 299 | <strong>&&</strong> is preferred over <strong>AND</strong> but either are acceptable, and a space should always precede and follow <strong>!</strong>.</p> |
| 300 | <code><strong>INCORRECT</strong>: |
| 301 | if ($foo || $bar) |
| 302 | if ($foo AND $bar) // okay but not recommended for common syntax highlighting applications |
| 303 | if (!$foo) |
| 304 | if (! is_array($foo)) |
| 305 | |
| 306 | <strong>CORRECT</strong>: |
| 307 | if ($foo OR $bar) |
| 308 | if ($foo && $bar) // recommended |
| 309 | if ( ! $foo) |
| 310 | if ( ! is_array($foo)) |
| 311 | </code> |
| 312 | </div> |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 313 | |
| 314 | |
| 315 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 316 | <h2><a name="comparing_return_values_and_typecasting"></a>Comparing Return Values and Typecasting</h2> |
| 317 | <div class="guidelineDetails"> |
| 318 | <p>Some PHP functions return FALSE on failure, but may also have a valid return value of "" or 0, which would evaluate to FALSE in loose comparisons. Be explicit by comparing the variable type when using these return values in conditionals to ensure the return value is indeed what you expect, and not a value that has an equivalent loose-type evaluation.</p> |
| 319 | <p>Use the same stringency in returning and checking your own variables. Use <strong>===</strong> and <strong>!==</strong> as necessary. |
| 320 | |
| 321 | <code><strong>INCORRECT</strong>: |
| 322 | // If 'foo' is at the beginning of the string, strpos will return a 0, |
| 323 | // resulting in this conditional evaluating as TRUE |
| 324 | if (strpos($str, 'foo') == FALSE) |
| 325 | |
| 326 | <strong>CORRECT</strong>: |
| 327 | if (strpos($str, 'foo') === FALSE) |
| 328 | </code> |
| 329 | |
| 330 | <code><strong>INCORRECT</strong>: |
| 331 | function build_string($str = "") |
| 332 | { |
| 333 | if ($str == "") // uh-oh! What if FALSE or the integer 0 is passed as an argument? |
| 334 | { |
| 335 | |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | <strong>CORRECT</strong>: |
| 340 | function build_string($str = "") |
| 341 | { |
| 342 | if ($str === "") |
| 343 | { |
| 344 | |
| 345 | } |
| 346 | }</code> |
| 347 | |
| 348 | <p>See also information regarding <a href="http://us3.php.net/manual/en/language.types.type-juggling.php#language.types.typecasting">typecasting</a>, which can be quite useful. Typecasting has a slightly different effect which may be desirable. When casting a variable as a string, for instance, NULL and boolean FALSE variables become empty strings, 0 (and other numbers) become strings of digits, and boolean TRUE becomes "1":</p> |
| 349 | |
| 350 | <code>$str = (string) $str; // cast $str as a string</code> |
| 351 | |
| 352 | </div> |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 353 | |
| 354 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 355 | <h2><a name="debugging_code"></a>Debugging Code</h2> |
| 356 | <div class="guidelineDetails"> |
| 357 | <p>No debugging code can be left in place for submitted add-ons unless it is commented out, i.e. no var_dump(), print_r(), die(), and exit() calls that were used while creating the add-on, unless they are commented out.</p> |
| 358 | |
| 359 | <code>// print_r($foo);</code> |
| 360 | </div> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 361 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 362 | |
| 363 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 364 | <h2><a name="whitespace_in_files"></a>Whitespace in Files</h2> |
| 365 | <div class="guidelineDetails"> |
Derek Allard | 5a8a455 | 2009-08-17 18:06:30 +0000 | [diff] [blame] | 366 | <p>No whitespace can precede the opening PHP tag or follow the closing PHP tag. Output is buffered, so whitespace in your files can cause output to begin before CodeIgniter outputs its content, leading to errors and an inability for CodeIgniter to send proper headers. In the examples below, select the text with your mouse to reveal the incorrect whitespace.</p> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 367 | |
| 368 | <p><strong>INCORRECT</strong>:</p> |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 369 | <code> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 370 | <?php |
| 371 | // ...there is whitespace and a linebreak above the opening PHP tag |
| 372 | // as well as whitespace after the closing PHP tag |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 373 | ?> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 374 | </code> |
| 375 | <p><strong>CORRECT</strong>:</p> |
| 376 | <code><?php |
| 377 | // this sample has no whitespace before or after the opening and closing PHP tags |
| 378 | ?></code> |
| 379 | |
| 380 | </div> |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 381 | |
| 382 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 383 | <h2><a name="compatibility"></a>Compatibility</h2> |
| 384 | <div class="guidelineDetails"> |
| 385 | <p>Unless specifically mentioned in your add-on's documentation, all code must be compatible with PHP version 4.3+. Additionally, do not use PHP functions that require non-default libraries to be installed unless your code contains an alternative method when the function is not available, or you implicitly document that your add-on requires said PHP libraries.</p> |
| 386 | </div> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 387 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 388 | |
| 389 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 390 | <h2><a name="class_and_file_names_using_common_words"></a>Class and File Names using Common Words</h2> |
| 391 | <div class="guidelineDetails"> |
| 392 | <p>When your class or filename is a common word, or might quite likely be identically named in another PHP script, provide a unique prefix to help prevent collision. Always realize that your end users may be running other add-ons or third party PHP scripts. Choose a prefix that is unique to your identity as a developer or company.</p> |
| 393 | |
| 394 | <code><strong>INCORRECT</strong>: |
| 395 | class Email pi.email.php |
| 396 | class Xml ext.xml.php |
| 397 | class Import mod.import.php |
| 398 | |
| 399 | <strong>CORRECT</strong>: |
| 400 | class Pre_email pi.pre_email.php |
| 401 | class Pre_xml ext.pre_xml.php |
| 402 | class Pre_import mod.pre_import.php |
| 403 | </code> |
| 404 | </div> |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 405 | |
| 406 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 407 | <h2><a name="database_table_names"></a>Database Table Names</h2> |
| 408 | <div class="guidelineDetails"> |
Derek Allard | 5a8a455 | 2009-08-17 18:06:30 +0000 | [diff] [blame] | 409 | <p>Any tables that your add-on might use must use the 'exp_' prefix, followed by a prefix uniquely identifying you as the developer or company, and then a short descriptive table name. You do not need to be concerned about the database prefix being used on the user's installation, as CodeIgniter's database class will automatically convert 'exp_' to what is actually being used.</p> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 410 | |
| 411 | <code><strong>INCORRECT</strong>: |
| 412 | email_addresses // missing both prefixes |
| 413 | pre_email_addresses // missing exp_ prefix |
| 414 | exp_email_addresses // missing unique prefix |
| 415 | |
| 416 | <strong>CORRECT</strong>: |
| 417 | exp_pre_email_addresses |
| 418 | </code> |
| 419 | |
| 420 | <p class="important"><strong>NOTE:</strong> Be mindful that MySQL has a limit of 64 characters for table names. This should not be an issue as table names that would exceed this would likely have unreasonable names. For instance, the following table name exceeds this limitation by one character. Silly, no? <strong>exp_pre_email_addresses_of_registered_users_in_seattle_washington</strong> |
| 421 | </div> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 422 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 423 | |
| 424 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 425 | <h2><a name="one_file_per_class"></a>One File per Class</h2> |
| 426 | <div class="guidelineDetails"> |
Derek Allard | 5a8a455 | 2009-08-17 18:06:30 +0000 | [diff] [blame] | 427 | <p>Use separate files for each class your add-on uses, unless the classes are <em>closely related</em>. An example of CodeIgniter files that contains multiple classes is the Database class file, which contains both the DB class and the DB_Cache class, and the Magpie plugin, which contains both the Magpie and Snoopy classes.</p> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 428 | </div> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 429 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 430 | |
| 431 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 432 | <h2><a name="whitespace"></a>Whitespace</h2> |
| 433 | <div class="guidelineDetails"> |
| 434 | <p>Use tabs for whitespace in your code, not spaces. This may seem like a small thing, but using tabs instead of whitespace allows the developer looking at your code to have indentation at levels that they prefer and customize in whatever application they use. And as a side benefit, it results in (slightly) more compact files, storing one tab character versus, say, four space characters.</p> |
| 435 | </div> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 436 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 437 | |
| 438 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 439 | <h2><a name="line_breaks"></a>Line Breaks</h2> |
| 440 | <div class="guidelineDetails"> |
| 441 | <p>Files must be saved with Unix line breaks. This is more of an issue for developers who work in Windows, but in any case ensure that your text editor is setup to save files with Unix line breaks.</p> |
| 442 | </div> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 443 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 444 | |
| 445 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 446 | <h2><a name="code_indenting"></a>Code Indenting</h2> |
| 447 | <div class="guidelineDetails"> |
| 448 | <p>Use Allman style indenting. With the exception of Class declarations, braces are always placed on a line by themselves, and indented at the same level as the control statement that "owns" them.</p> |
| 449 | |
| 450 | <code><strong>INCORRECT</strong>: |
| 451 | function foo($bar) { |
| 452 | // ... |
| 453 | } |
| 454 | |
| 455 | foreach ($arr as $key => $val) { |
| 456 | // ... |
| 457 | } |
| 458 | |
| 459 | if ($foo == $bar) { |
| 460 | // ... |
| 461 | } else { |
| 462 | // ... |
| 463 | } |
| 464 | |
| 465 | for ($i = 0; $i < 10; $i++) |
| 466 | { |
| 467 | for ($j = 0; $j < 10; $j++) |
| 468 | { |
| 469 | // ... |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | <strong>CORRECT</strong>: |
| 474 | function foo($bar) |
| 475 | { |
| 476 | // ... |
| 477 | } |
| 478 | |
| 479 | foreach ($arr as $key => $val) |
| 480 | { |
| 481 | // ... |
| 482 | } |
| 483 | |
| 484 | if ($foo == $bar) |
| 485 | { |
| 486 | // ... |
| 487 | } |
| 488 | else |
| 489 | { |
| 490 | // ... |
| 491 | } |
| 492 | |
| 493 | for ($i = 0; $i < 10; $i++) |
| 494 | { |
| 495 | for ($j = 0; $j < 10; $j++) |
| 496 | { |
| 497 | // ... |
| 498 | } |
| 499 | }</code> |
| 500 | </div> |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 501 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 502 | |
| 503 | <h2><a name="bracket_spacing"></a>Bracket and Parenthetic Spacing</h2> |
| 504 | <div class="guidelineDetails"> |
| 505 | <p>In general, parenthesis and brackets should not use any additional spaces. The exception is that a space should always follow PHP control structures that accept arguments with parenthesis (declare, do-while, elseif, for, foreach, if, switch, while), to help distinguish them from functions and increase readability.</p> |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 506 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 507 | <code>INCORRECT: |
| 508 | $arr[ $foo ] = 'foo'; |
| 509 | |
| 510 | CORRECT: |
| 511 | $arr[$foo] = 'foo'; // no spaces around array keys |
| 512 | |
| 513 | |
| 514 | INCORRECT: |
| 515 | function foo ( $bar ) |
| 516 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 517 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | CORRECT: |
| 521 | function foo($bar) // no spaces around parenthesis in function declarations |
| 522 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 523 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | |
| 527 | INCORRECT: |
| 528 | foreach( $query->result() as $row ) |
| 529 | |
| 530 | CORRECT: |
| 531 | foreach ($query->result() as $row) // single space following PHP control structures, but not in interior parenthesis |
| 532 | </code> |
| 533 | </div> |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 534 | |
| 535 | |
| 536 | |
Derek Allard | 2f304d6 | 2010-07-05 07:40:26 -0400 | [diff] [blame] | 537 | <h2><a name="localized_text"></a>Localized Text</h2> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 538 | <div class="guidelineDetails"> |
Derek Allard | 2f304d6 | 2010-07-05 07:40:26 -0400 | [diff] [blame] | 539 | <p>Any text that is output in the control panel should use language variables in your lang file to allow localization.</p> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 540 | |
| 541 | <code>INCORRECT: |
| 542 | return "Invalid Selection"; |
| 543 | |
| 544 | CORRECT: |
Derek Allard | 2f304d6 | 2010-07-05 07:40:26 -0400 | [diff] [blame] | 545 | return $this->lang->line('invalid_selection');</code> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 546 | </div> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 547 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 548 | |
| 549 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 550 | <h2><a name="private_methods_and_variables"></a>Private Methods and Variables</h2> |
| 551 | <div class="guidelineDetails"> |
| 552 | <p>Methods and variables that are only accessed internally by your class, such as utility and helper functions that your public methods use for code abstraction, should be prefixed with an underscore.</p> |
| 553 | |
| 554 | <code>convert_text() // public method |
| 555 | _convert_text() // private method</code> |
| 556 | </div> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 557 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 558 | |
| 559 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 560 | <h2><a name="php_errors"></a>PHP Errors</h2> |
| 561 | <div class="guidelineDetails"> |
| 562 | <p>Code must run error free and not rely on warnings and notices to be hidden to meet this requirement. For instance, never access a variable that you did not set yourself (such as $_POST array keys) without first checking to see that it isset().</p> |
| 563 | |
| 564 | <p>Make sure that while developing your add-on, error reporting is enabled for ALL users, and that display_errors is enabled in the PHP environment. You can check this setting with:</p> |
| 565 | |
| 566 | <code>if (ini_get('display_errors') == 1) |
| 567 | { |
| 568 | exit "Enabled"; |
| 569 | }</code> |
| 570 | |
| 571 | <p>On some servers where display_errors is disabled, and you do not have the ability to change this in the php.ini, you can often enable it with:</p> |
| 572 | |
| 573 | <code>ini_set('display_errors', 1);</code> |
| 574 | |
| 575 | <p class="important"><strong>NOTE:</strong> Setting the <a href="http://us.php.net/manual/en/ref.errorfunc.php#ini.display-errors">display_errors</a> setting with ini_set() at runtime is not identical to having it enabled in the PHP environment. Namely, it will not have any effect if the script has fatal errors</p> |
| 576 | </div> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 577 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 578 | |
| 579 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 580 | <h2><a name="short_open_tags"></a>Short Open Tags</h2> |
| 581 | <div class="guidelineDetails"> |
| 582 | <p>Always use full PHP opening tags, in case a server does not have short_open_tag enabled.</p> |
| 583 | |
| 584 | <code><strong>INCORRECT</strong>: |
| 585 | <? echo $foo; ?> |
| 586 | |
| 587 | <?=$foo?> |
| 588 | |
| 589 | <strong>CORRECT</strong>: |
| 590 | <?php echo $foo; ?></code> |
| 591 | </div> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 592 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 593 | |
| 594 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 595 | <h2><a name="one_statement_per_line"></a>One Statement Per Line</h2> |
| 596 | <div class="guidelineDetails"> |
| 597 | <p>Never combine statements on one line.</p> |
| 598 | |
| 599 | <code><strong>INCORRECT</strong>: |
| 600 | $foo = 'this'; $bar = 'that'; $bat = str_replace($foo, $bar, $bag); |
| 601 | |
| 602 | <strong>CORRECT</strong>: |
| 603 | $foo = 'this'; |
| 604 | $bar = 'that'; |
| 605 | $bat = str_replace($foo, $bar, $bag); |
| 606 | </code> |
| 607 | </div> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 608 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 609 | |
| 610 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 611 | <h2><a name="strings"></a>Strings</h2> |
| 612 | <div class="guidelineDetails"> |
| 613 | <p>Always use single quoted strings unless you need variables parsed, and in cases where you do need variables parsed, use braces to prevent greedy token parsing. You may also use double-quoted strings if the string contains single quotes, so you do not have to use escape characters.</p> |
| 614 | |
| 615 | <code><strong>INCORRECT</strong>: |
| 616 | "My String" // no variable parsing, so no use for double quotes |
| 617 | "My string $foo" // needs braces |
| 618 | 'SELECT foo FROM bar WHERE baz = \'bag\'' // ugly |
| 619 | |
| 620 | <strong>CORRECT</strong>: |
| 621 | 'My String' |
| 622 | "My string {$foo}" |
| 623 | "SELECT foo FROM bar WHERE baz = 'bag'"</code> |
| 624 | </div> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 625 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 626 | |
| 627 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 628 | <h2><a name="sql_queries"></a>SQL Queries</h2> |
| 629 | <div class="guidelineDetails"> |
| 630 | <p>MySQL keywords are always capitalized: SELECT, INSERT, UPDATE, WHERE, AS, JOIN, ON, IN, etc.</p> |
| 631 | |
| 632 | <p>Break up long queries into multiple lines for legibility, preferably breaking for each clause.</p> |
| 633 | |
| 634 | <code><strong>INCORRECT</strong>: |
| 635 | // keywords are lowercase and query is too long for |
| 636 | // a single line (... indicates continuation of line) |
| 637 | $query = $this->db->query("select foo, bar, baz, foofoo, foobar as raboof, foobaz from exp_pre_email_addresses |
| 638 | ...where foo != 'oof' and baz != 'zab' order by foobaz limit 5, 100"); |
| 639 | |
| 640 | <strong>CORRECT</strong>: |
| 641 | $query = $this->db->query("SELECT foo, bar, baz, foofoo, foobar AS raboof, foobaz |
| 642 | FROM exp_pre_email_addresses |
| 643 | WHERE foo != 'oof' |
| 644 | AND baz != 'zab' |
| 645 | ORDER BY foobaz |
| 646 | LIMIT 5, 100");</code> |
| 647 | </div> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 648 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 649 | |
| 650 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 651 | <h2><a name="default_function_arguments"></a>Default Function Arguments</h2> |
| 652 | <div class="guidelineDetails"> |
| 653 | <p>Whenever appropriate, provide function argument defaults, which helps prevent PHP errors with mistaken calls and provides common fallback values which can save a few lines of code. Example:</p> |
| 654 | |
| 655 | <code>function foo($bar = '', $baz = FALSE)</code> |
| 656 | </div> |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 657 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 658 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 659 | |
| 660 | </div> |
| 661 | |
| 662 | |
| 663 | |
| 664 | </div> |
| 665 | <!-- END CONTENT --> |
| 666 | |
| 667 | |
| 668 | <div id="footer"> |
| 669 | <p> |
| 670 | Previous Topic: <a href="security.html">Security</a> |
| 671 | · |
| 672 | <a href="#top">Top of Page</a> · |
| 673 | <a href="../index.html">User Guide Home</a> · |
| 674 | Next Topic: <a href="../doc_style/index.html">Writing Documentation</a> |
| 675 | </p> |
Derek Jones | d6d70e3 | 2010-03-29 10:10:27 -0500 | [diff] [blame] | 676 | <p><a href="http://codeigniter.com">CodeIgniter</a> · Copyright © 2006-2010 · <a href="http://ellislab.com/">EllisLab, Inc.</a></p> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 677 | </div> |
| 678 | |
| 679 | </body> |
Rick Ellis | 5a9fd7a | 2008-08-31 05:58:32 +0000 | [diff] [blame] | 680 | </html> |