Derek Jones | 619b122 | 2011-10-10 16:26:27 -0500 | [diff] [blame] | 1 | ############### |
| 2 | PHP Style Guide |
| 3 | ############### |
| 4 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 5 | |
Eric Roberts | 4addd5e | 2013-02-25 14:14:05 -0600 | [diff] [blame] | 6 | The following page describes the coding styles adhered to when |
| 7 | contributing to the development of CodeIgniter. There is no requirement |
| 8 | to use these styles in your own CodeIgniter application, though they |
| 9 | are recommended. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 10 | |
| 11 | .. contents:: Table of Contents |
| 12 | |
| 13 | File Format |
| 14 | =========== |
| 15 | |
| 16 | Files should be saved with Unicode (UTF-8) encoding. The BOM should |
| 17 | *not* be used. Unlike UTF-16 and UTF-32, there's no byte order to |
| 18 | indicate in a UTF-8 encoded file, and the BOM can have a negative side |
| 19 | effect in PHP of sending output, preventing the application from being |
| 20 | able to set its own headers. Unix line endings should be used (LF). |
| 21 | |
| 22 | Here is how to apply these settings in some of the more common text |
| 23 | editors. Instructions for your text editor may vary; check your text |
| 24 | editor's documentation. |
| 25 | |
| 26 | TextMate |
| 27 | '''''''' |
| 28 | |
| 29 | #. Open the Application Preferences |
| 30 | #. Click Advanced, and then the "Saving" tab |
| 31 | #. In "File Encoding", select "UTF-8 (recommended)" |
| 32 | #. In "Line Endings", select "LF (recommended)" |
| 33 | #. *Optional:* Check "Use for existing files as well" if you wish to |
| 34 | modify the line endings of files you open to your new preference. |
| 35 | |
| 36 | BBEdit |
| 37 | '''''' |
| 38 | |
| 39 | #. Open the Application Preferences |
| 40 | #. Select "Text Encodings" on the left. |
| 41 | #. In "Default text encoding for new documents", select "Unicode (UTF-8, |
| 42 | no BOM)" |
| 43 | #. *Optional:* In "If file's encoding can't be guessed, use", select |
| 44 | "Unicode (UTF-8, no BOM)" |
| 45 | #. Select "Text Files" on the left. |
| 46 | #. In "Default line breaks", select "Mac OS X and Unix (LF)" |
| 47 | |
| 48 | PHP Closing Tag |
| 49 | =============== |
| 50 | |
| 51 | The PHP closing tag on a PHP document **?>** is optional to the PHP |
| 52 | parser. However, if used, any whitespace following the closing tag, |
| 53 | whether introduced by the developer, user, or an FTP application, can |
| 54 | cause unwanted output, PHP errors, or if the latter are suppressed, |
| 55 | blank pages. For this reason, all PHP files should **OMIT** the closing |
| 56 | PHP tag, and instead use a comment block to mark the end of file and |
Andrey Andreev | 93f5c5d | 2012-11-22 13:26:07 +0200 | [diff] [blame] | 57 | its location relative to the application root. This allows you to still |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 58 | identify a file as being complete and not truncated. |
| 59 | |
Derek Jones | 129c181 | 2011-10-05 17:15:44 -0500 | [diff] [blame] | 60 | **INCORRECT**:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 61 | |
Derek Jones | 129c181 | 2011-10-05 17:15:44 -0500 | [diff] [blame] | 62 | <?php |
| 63 | |
| 64 | echo "Here's my code!"; |
| 65 | |
| 66 | ?> |
| 67 | |
| 68 | **CORRECT**:: |
| 69 | |
| 70 | <?php |
| 71 | |
| 72 | echo "Here's my code!"; |
| 73 | |
| 74 | /* End of file myfile.php */ |
| 75 | /* Location: ./system/modules/mymodule/myfile.php */ |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 76 | |
Eric Roberts | 4addd5e | 2013-02-25 14:14:05 -0600 | [diff] [blame] | 77 | .. note:: There should be no empty line or newline character(s) following |
| 78 | the closing comments. If you happen to see one when |
| 79 | submitting a pull request, please check your IDE settings and fix it. |
| 80 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 81 | Class and Method Naming |
| 82 | ======================= |
| 83 | |
| 84 | Class names should always start with an uppercase letter. Multiple words |
Eric Roberts | 4addd5e | 2013-02-25 14:14:05 -0600 | [diff] [blame] | 85 | should be separated with an underscore, and not CamelCased. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 86 | |
Derek Jones | 129c181 | 2011-10-05 17:15:44 -0500 | [diff] [blame] | 87 | **INCORRECT**:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 88 | |
Derek Jones | 129c181 | 2011-10-05 17:15:44 -0500 | [diff] [blame] | 89 | class superclass |
| 90 | class SuperClass |
| 91 | |
| 92 | **CORRECT**:: |
| 93 | |
| 94 | class Super_class |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 95 | |
| 96 | :: |
| 97 | |
Derek Jones | 129c181 | 2011-10-05 17:15:44 -0500 | [diff] [blame] | 98 | class Super_class { |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 99 | |
Andrey Andreev | d8e1ac7 | 2012-03-26 22:22:37 +0300 | [diff] [blame] | 100 | public function __construct() |
Derek Jones | 129c181 | 2011-10-05 17:15:44 -0500 | [diff] [blame] | 101 | { |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 102 | |
Derek Jones | 129c181 | 2011-10-05 17:15:44 -0500 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
Eric Roberts | 4addd5e | 2013-02-25 14:14:05 -0600 | [diff] [blame] | 106 | Class methods should be entirely lowercased and named to clearly |
| 107 | indicate their function, preferably including a verb. Try to avoid |
Eric Roberts | d746f26 | 2013-02-25 19:55:49 -0600 | [diff] [blame] | 108 | overly long and verbose names. Multiple words should be separated |
| 109 | with an underscore. |
Derek Jones | 129c181 | 2011-10-05 17:15:44 -0500 | [diff] [blame] | 110 | |
| 111 | **INCORRECT**:: |
| 112 | |
| 113 | function fileproperties() // not descriptive and needs underscore separator |
| 114 | function fileProperties() // not descriptive and uses CamelCase |
| 115 | function getfileproperties() // Better! But still missing underscore separator |
| 116 | function getFileProperties() // uses CamelCase |
| 117 | function get_the_file_properties_from_the_file() // wordy |
| 118 | |
| 119 | **CORRECT**:: |
| 120 | |
| 121 | function get_file_properties() // descriptive, underscore separator, and all lowercase letters |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 122 | |
| 123 | Variable Names |
| 124 | ============== |
| 125 | |
Eric Roberts | 4addd5e | 2013-02-25 14:14:05 -0600 | [diff] [blame] | 126 | The guidelines for variable naming are very similar to those used for |
| 127 | class methods. Variables should contain only lowercase letters, |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 128 | use underscore separators, and be reasonably named to indicate their |
| 129 | purpose and contents. Very short, non-word variables should only be used |
| 130 | as iterators in for() loops. |
| 131 | |
Derek Jones | 129c181 | 2011-10-05 17:15:44 -0500 | [diff] [blame] | 132 | **INCORRECT**:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 133 | |
Derek Jones | 129c181 | 2011-10-05 17:15:44 -0500 | [diff] [blame] | 134 | $j = 'foo'; // single letter variables should only be used in for() loops |
| 135 | $Str // contains uppercase letters |
| 136 | $bufferedText // uses CamelCasing, and could be shortened without losing semantic meaning |
| 137 | $groupid // multiple words, needs underscore separator |
| 138 | $name_of_last_city_used // too long |
| 139 | |
| 140 | **CORRECT**:: |
| 141 | |
| 142 | for ($j = 0; $j < 10; $j++) |
| 143 | $str |
| 144 | $buffer |
| 145 | $group_id |
| 146 | $last_city |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 147 | |
| 148 | Commenting |
| 149 | ========== |
| 150 | |
| 151 | In general, code should be commented prolifically. It not only helps |
| 152 | describe the flow and intent of the code for less experienced |
| 153 | programmers, but can prove invaluable when returning to your own code |
| 154 | months down the line. There is not a required format for comments, but |
| 155 | the following are recommended. |
| 156 | |
| 157 | `DocBlock <http://manual.phpdoc.org/HTMLSmartyConverter/HandS/phpDocumentor/tutorial_phpDocumentor.howto.pkg.html#basics.docblock>`_ |
Timothy Warren | bb8ae01 | 2012-04-20 10:31:51 -0400 | [diff] [blame] | 158 | style comments preceding class, method, and property declarations so they can be |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 159 | picked up by IDEs:: |
| 160 | |
Derek Jones | 129c181 | 2011-10-05 17:15:44 -0500 | [diff] [blame] | 161 | /** |
| 162 | * Super Class |
| 163 | * |
| 164 | * @package Package Name |
| 165 | * @subpackage Subpackage |
| 166 | * @category Category |
| 167 | * @author Author Name |
| 168 | * @link http://example.com |
| 169 | */ |
| 170 | class Super_class { |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 171 | |
| 172 | :: |
| 173 | |
Derek Jones | 129c181 | 2011-10-05 17:15:44 -0500 | [diff] [blame] | 174 | /** |
| 175 | * Encodes string for use in XML |
| 176 | * |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 177 | * @param string $str Input string |
Derek Jones | 129c181 | 2011-10-05 17:15:44 -0500 | [diff] [blame] | 178 | * @return string |
| 179 | */ |
| 180 | function xml_encode($str) |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 181 | |
Timothy Warren | bb8ae01 | 2012-04-20 10:31:51 -0400 | [diff] [blame] | 182 | :: |
| 183 | |
| 184 | /** |
| 185 | * Data for class manipulation |
| 186 | * |
| 187 | * @var array |
| 188 | */ |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 189 | public $data = array(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 190 | |
| 191 | Use single line comments within code, leaving a blank line between large |
| 192 | comment blocks and code. |
| 193 | |
| 194 | :: |
| 195 | |
Derek Jones | 129c181 | 2011-10-05 17:15:44 -0500 | [diff] [blame] | 196 | // break up the string by newlines |
| 197 | $parts = explode("\n", $str); |
| 198 | |
| 199 | // A longer comment that needs to give greater detail on what is |
| 200 | // occurring and why can use multiple single-line comments. Try to |
| 201 | // keep the width reasonable, around 70 characters is the easiest to |
| 202 | // read. Don't hesitate to link to permanent external resources |
| 203 | // that may provide greater detail: |
| 204 | // |
| 205 | // http://example.com/information_about_something/in_particular/ |
| 206 | |
| 207 | $parts = $this->foo($parts); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 208 | |
| 209 | Constants |
| 210 | ========= |
| 211 | |
| 212 | Constants follow the same guidelines as do variables, except constants |
| 213 | should always be fully uppercase. *Always use CodeIgniter constants when |
| 214 | appropriate, i.e. SLASH, LD, RD, PATH_CACHE, etc.* |
| 215 | |
Derek Jones | 129c181 | 2011-10-05 17:15:44 -0500 | [diff] [blame] | 216 | **INCORRECT**:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 217 | |
Derek Jones | 129c181 | 2011-10-05 17:15:44 -0500 | [diff] [blame] | 218 | myConstant // missing underscore separator and not fully uppercase |
| 219 | N // no single-letter constants |
| 220 | S_C_VER // not descriptive |
| 221 | $str = str_replace('{foo}', 'bar', $str); // should use LD and RD constants |
| 222 | |
| 223 | **CORRECT**:: |
| 224 | |
| 225 | MY_CONSTANT |
| 226 | NEWLINE |
| 227 | SUPER_CLASS_VERSION |
| 228 | $str = str_replace(LD.'foo'.RD, 'bar', $str); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 229 | |
| 230 | TRUE, FALSE, and NULL |
| 231 | ===================== |
| 232 | |
| 233 | **TRUE**, **FALSE**, and **NULL** keywords should always be fully |
| 234 | uppercase. |
| 235 | |
Derek Jones | 129c181 | 2011-10-05 17:15:44 -0500 | [diff] [blame] | 236 | **INCORRECT**:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 237 | |
Derek Jones | 129c181 | 2011-10-05 17:15:44 -0500 | [diff] [blame] | 238 | if ($foo == true) |
| 239 | $bar = false; |
| 240 | function foo($bar = null) |
| 241 | |
| 242 | **CORRECT**:: |
| 243 | |
| 244 | if ($foo == TRUE) |
| 245 | $bar = FALSE; |
| 246 | function foo($bar = NULL) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 247 | |
| 248 | Logical Operators |
| 249 | ================= |
| 250 | |
Eric Roberts | d746f26 | 2013-02-25 19:55:49 -0600 | [diff] [blame] | 251 | Use of the ``||`` "or" comparison operator is discouraged, as its clarity |
Eric Roberts | 4addd5e | 2013-02-25 14:14:05 -0600 | [diff] [blame] | 252 | on some output devices is low (looking like the number 11, for instance). |
Eric Roberts | d746f26 | 2013-02-25 19:55:49 -0600 | [diff] [blame] | 253 | ``&&`` is preferred over ``AND`` but either are acceptable, and a space should |
| 254 | always precede and follow ``!``. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 255 | |
Derek Jones | 129c181 | 2011-10-05 17:15:44 -0500 | [diff] [blame] | 256 | **INCORRECT**:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 257 | |
Derek Jones | 129c181 | 2011-10-05 17:15:44 -0500 | [diff] [blame] | 258 | if ($foo || $bar) |
| 259 | if ($foo AND $bar) // okay but not recommended for common syntax highlighting applications |
| 260 | if (!$foo) |
| 261 | if (! is_array($foo)) |
| 262 | |
| 263 | **CORRECT**:: |
| 264 | |
| 265 | if ($foo OR $bar) |
| 266 | if ($foo && $bar) // recommended |
| 267 | if ( ! $foo) |
| 268 | if ( ! is_array($foo)) |
| 269 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 270 | |
| 271 | Comparing Return Values and Typecasting |
| 272 | ======================================= |
| 273 | |
| 274 | Some PHP functions return FALSE on failure, but may also have a valid |
| 275 | return value of "" or 0, which would evaluate to FALSE in loose |
| 276 | comparisons. Be explicit by comparing the variable type when using these |
| 277 | return values in conditionals to ensure the return value is indeed what |
| 278 | you expect, and not a value that has an equivalent loose-type |
| 279 | evaluation. |
| 280 | |
| 281 | Use the same stringency in returning and checking your own variables. |
| 282 | Use **===** and **!==** as necessary. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 283 | |
Derek Jones | 129c181 | 2011-10-05 17:15:44 -0500 | [diff] [blame] | 284 | **INCORRECT**:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 285 | |
Derek Jones | 129c181 | 2011-10-05 17:15:44 -0500 | [diff] [blame] | 286 | // If 'foo' is at the beginning of the string, strpos will return a 0, |
| 287 | // resulting in this conditional evaluating as TRUE |
| 288 | if (strpos($str, 'foo') == FALSE) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 289 | |
Derek Jones | 129c181 | 2011-10-05 17:15:44 -0500 | [diff] [blame] | 290 | **CORRECT**:: |
| 291 | |
| 292 | if (strpos($str, 'foo') === FALSE) |
| 293 | |
| 294 | **INCORRECT**:: |
| 295 | |
| 296 | function build_string($str = "") |
| 297 | { |
| 298 | if ($str == "") // uh-oh! What if FALSE or the integer 0 is passed as an argument? |
| 299 | { |
| 300 | |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | **CORRECT**:: |
| 305 | |
| 306 | function build_string($str = "") |
| 307 | { |
| 308 | if ($str === "") |
| 309 | { |
| 310 | |
| 311 | } |
| 312 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 313 | |
| 314 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 315 | See also information regarding `typecasting |
| 316 | <http://php.net/manual/en/language.types.type-juggling.php#language.types.typecasting>`_, |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 317 | which can be quite useful. Typecasting has a slightly different effect |
| 318 | which may be desirable. When casting a variable as a string, for |
| 319 | instance, NULL and boolean FALSE variables become empty strings, 0 (and |
| 320 | other numbers) become strings of digits, and boolean TRUE becomes "1":: |
| 321 | |
| 322 | $str = (string) $str; // cast $str as a string |
| 323 | |
| 324 | Debugging Code |
| 325 | ============== |
| 326 | |
Eric Roberts | 4addd5e | 2013-02-25 14:14:05 -0600 | [diff] [blame] | 327 | Do not leave debugging code in your submissions, even when commented out. |
Eric Roberts | d746f26 | 2013-02-25 19:55:49 -0600 | [diff] [blame] | 328 | Things such as ``var_dump()``, ``print_r()``, ``die()``/``exit()`` should not be included |
Eric Roberts | 4addd5e | 2013-02-25 14:14:05 -0600 | [diff] [blame] | 329 | in your code unless it serves a specific purpose other than debugging. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 330 | |
| 331 | Whitespace in Files |
| 332 | =================== |
| 333 | |
| 334 | No whitespace can precede the opening PHP tag or follow the closing PHP |
| 335 | tag. Output is buffered, so whitespace in your files can cause output to |
| 336 | begin before CodeIgniter outputs its content, leading to errors and an |
Eric Roberts | 4addd5e | 2013-02-25 14:14:05 -0600 | [diff] [blame] | 337 | inability for CodeIgniter to send proper headers. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 338 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 339 | Compatibility |
| 340 | ============= |
| 341 | |
Eric Roberts | 4addd5e | 2013-02-25 14:14:05 -0600 | [diff] [blame] | 342 | CodeIgniter requires a minimum PHP version of 5.2.4. Your code must either |
| 343 | be compatible with this minimum requirement, provide a suitable fallback, |
| 344 | or be an optional feature that dies quietly without affecting a user's |
| 345 | application. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 346 | |
Eric Roberts | 4addd5e | 2013-02-25 14:14:05 -0600 | [diff] [blame] | 347 | Additionally, do not use PHP functions that require non-default libraries |
| 348 | to be installed unless your code contains an alternative method when the |
| 349 | function is not available. |
Derek Jones | 129c181 | 2011-10-05 17:15:44 -0500 | [diff] [blame] | 350 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 351 | One File per Class |
| 352 | ================== |
| 353 | |
Eric Roberts | 4addd5e | 2013-02-25 14:14:05 -0600 | [diff] [blame] | 354 | Use separate files for each class, unless the classes are *closely related*. |
Eric Roberts | d746f26 | 2013-02-25 19:55:49 -0600 | [diff] [blame] | 355 | An example of a CodeIgniter file that contains multiple classes is the |
| 356 | Xmlrpc library file. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 357 | |
| 358 | Whitespace |
| 359 | ========== |
| 360 | |
| 361 | Use tabs for whitespace in your code, not spaces. This may seem like a |
| 362 | small thing, but using tabs instead of whitespace allows the developer |
| 363 | looking at your code to have indentation at levels that they prefer and |
| 364 | customize in whatever application they use. And as a side benefit, it |
| 365 | results in (slightly) more compact files, storing one tab character |
| 366 | versus, say, four space characters. |
| 367 | |
| 368 | Line Breaks |
| 369 | =========== |
| 370 | |
| 371 | Files must be saved with Unix line breaks. This is more of an issue for |
| 372 | developers who work in Windows, but in any case ensure that your text |
| 373 | editor is setup to save files with Unix line breaks. |
| 374 | |
| 375 | Code Indenting |
| 376 | ============== |
| 377 | |
| 378 | Use Allman style indenting. With the exception of Class declarations, |
| 379 | braces are always placed on a line by themselves, and indented at the |
| 380 | same level as the control statement that "owns" them. |
| 381 | |
Derek Jones | 129c181 | 2011-10-05 17:15:44 -0500 | [diff] [blame] | 382 | **INCORRECT**:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 383 | |
Derek Jones | 129c181 | 2011-10-05 17:15:44 -0500 | [diff] [blame] | 384 | function foo($bar) { |
| 385 | // ... |
| 386 | } |
| 387 | |
| 388 | foreach ($arr as $key => $val) { |
| 389 | // ... |
| 390 | } |
| 391 | |
| 392 | if ($foo == $bar) { |
| 393 | // ... |
| 394 | } else { |
| 395 | // ... |
| 396 | } |
| 397 | |
| 398 | for ($i = 0; $i < 10; $i++) |
| 399 | { |
| 400 | for ($j = 0; $j < 10; $j++) |
| 401 | { |
| 402 | // ... |
| 403 | } |
| 404 | } |
Timothy Warren | 82c8307 | 2012-01-26 19:02:05 -0500 | [diff] [blame] | 405 | |
| 406 | try { |
| 407 | // ... |
| 408 | } |
| 409 | catch() { |
| 410 | // ... |
| 411 | } |
Derek Jones | 129c181 | 2011-10-05 17:15:44 -0500 | [diff] [blame] | 412 | |
| 413 | **CORRECT**:: |
| 414 | |
| 415 | function foo($bar) |
| 416 | { |
| 417 | // ... |
| 418 | } |
| 419 | |
| 420 | foreach ($arr as $key => $val) |
| 421 | { |
| 422 | // ... |
| 423 | } |
| 424 | |
| 425 | if ($foo == $bar) |
| 426 | { |
| 427 | // ... |
| 428 | } |
| 429 | else |
| 430 | { |
| 431 | // ... |
| 432 | } |
| 433 | |
| 434 | for ($i = 0; $i < 10; $i++) |
| 435 | { |
| 436 | for ($j = 0; $j < 10; $j++) |
| 437 | { |
| 438 | // ... |
| 439 | } |
| 440 | } |
Timothy Warren | 82c8307 | 2012-01-26 19:02:05 -0500 | [diff] [blame] | 441 | |
| 442 | try |
| 443 | { |
| 444 | // ... |
| 445 | } |
| 446 | catch() |
| 447 | { |
| 448 | // ... |
| 449 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 450 | |
| 451 | Bracket and Parenthetic Spacing |
| 452 | =============================== |
| 453 | |
| 454 | In general, parenthesis and brackets should not use any additional |
| 455 | spaces. The exception is that a space should always follow PHP control |
| 456 | structures that accept arguments with parenthesis (declare, do-while, |
| 457 | elseif, for, foreach, if, switch, while), to help distinguish them from |
| 458 | functions and increase readability. |
| 459 | |
Derek Jones | 129c181 | 2011-10-05 17:15:44 -0500 | [diff] [blame] | 460 | **INCORRECT**:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 461 | |
Derek Jones | 129c181 | 2011-10-05 17:15:44 -0500 | [diff] [blame] | 462 | $arr[ $foo ] = 'foo'; |
| 463 | |
| 464 | **CORRECT**:: |
| 465 | |
| 466 | $arr[$foo] = 'foo'; // no spaces around array keys |
| 467 | |
| 468 | **INCORRECT**:: |
| 469 | |
| 470 | function foo ( $bar ) |
| 471 | { |
| 472 | |
| 473 | } |
| 474 | |
| 475 | **CORRECT**:: |
| 476 | |
| 477 | function foo($bar) // no spaces around parenthesis in function declarations |
| 478 | { |
| 479 | |
| 480 | } |
| 481 | |
| 482 | **INCORRECT**:: |
| 483 | |
| 484 | foreach( $query->result() as $row ) |
| 485 | |
| 486 | **CORRECT**:: |
| 487 | |
| 488 | foreach ($query->result() as $row) // single space following PHP control structures, but not in interior parenthesis |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 489 | |
| 490 | Localized Text |
| 491 | ============== |
| 492 | |
Eric Roberts | 4addd5e | 2013-02-25 14:14:05 -0600 | [diff] [blame] | 493 | CodeIgniter libraries should take advantage of corresponding language files |
| 494 | whenever possible. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 495 | |
Derek Jones | 129c181 | 2011-10-05 17:15:44 -0500 | [diff] [blame] | 496 | **INCORRECT**:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 497 | |
Derek Jones | 129c181 | 2011-10-05 17:15:44 -0500 | [diff] [blame] | 498 | return "Invalid Selection"; |
| 499 | |
| 500 | **CORRECT**:: |
| 501 | |
| 502 | return $this->lang->line('invalid_selection'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 503 | |
| 504 | Private Methods and Variables |
| 505 | ============================= |
| 506 | |
Eric Roberts | 4addd5e | 2013-02-25 14:14:05 -0600 | [diff] [blame] | 507 | Methods and variables that are only accessed internally, |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 508 | such as utility and helper functions that your public methods use for |
| 509 | code abstraction, should be prefixed with an underscore. |
| 510 | |
| 511 | :: |
| 512 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 513 | public function convert_text() |
| 514 | private function _convert_text() |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 515 | |
| 516 | PHP Errors |
| 517 | ========== |
| 518 | |
| 519 | Code must run error free and not rely on warnings and notices to be |
| 520 | hidden to meet this requirement. For instance, never access a variable |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 521 | that you did not set yourself (such as ``$_POST`` array keys) without first |
| 522 | checking to see that it ``isset()``. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 523 | |
Eric Roberts | 4addd5e | 2013-02-25 14:14:05 -0600 | [diff] [blame] | 524 | Make sure that your dev environment has error reporting enabled |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 525 | for ALL users, and that display_errors is enabled in the PHP |
| 526 | environment. You can check this setting with:: |
| 527 | |
Derek Jones | 129c181 | 2011-10-05 17:15:44 -0500 | [diff] [blame] | 528 | if (ini_get('display_errors') == 1) |
| 529 | { |
| 530 | exit "Enabled"; |
| 531 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 532 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 533 | On some servers where *display_errors* is disabled, and you do not have |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 534 | the ability to change this in the php.ini, you can often enable it with:: |
| 535 | |
| 536 | ini_set('display_errors', 1); |
| 537 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 538 | .. note:: Setting the `display_errors |
| 539 | <http://php.net/manual/en/ref.errorfunc.php#ini.display-errors>`_ |
| 540 | setting with ``ini_set()`` at runtime is not identical to having |
| 541 | it enabled in the PHP environment. Namely, it will not have any |
| 542 | effect if the script has fatal errors. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 543 | |
| 544 | Short Open Tags |
| 545 | =============== |
| 546 | |
| 547 | Always use full PHP opening tags, in case a server does not have |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 548 | *short_open_tag* enabled. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 549 | |
Derek Jones | 129c181 | 2011-10-05 17:15:44 -0500 | [diff] [blame] | 550 | **INCORRECT**:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 551 | |
Derek Jones | 129c181 | 2011-10-05 17:15:44 -0500 | [diff] [blame] | 552 | <? echo $foo; ?> |
| 553 | |
| 554 | <?=$foo?> |
| 555 | |
| 556 | **CORRECT**:: |
| 557 | |
| 558 | <?php echo $foo; ?> |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 559 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 560 | .. note:: PHP 5.4 will always have the **<?=** tag available. |
| 561 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 562 | One Statement Per Line |
| 563 | ====================== |
| 564 | |
| 565 | Never combine statements on one line. |
| 566 | |
Derek Jones | 129c181 | 2011-10-05 17:15:44 -0500 | [diff] [blame] | 567 | **INCORRECT**:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 568 | |
Derek Jones | 129c181 | 2011-10-05 17:15:44 -0500 | [diff] [blame] | 569 | $foo = 'this'; $bar = 'that'; $bat = str_replace($foo, $bar, $bag); |
| 570 | |
| 571 | **CORRECT**:: |
| 572 | |
| 573 | $foo = 'this'; |
| 574 | $bar = 'that'; |
| 575 | $bat = str_replace($foo, $bar, $bag); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 576 | |
| 577 | Strings |
| 578 | ======= |
| 579 | |
| 580 | Always use single quoted strings unless you need variables parsed, and |
| 581 | in cases where you do need variables parsed, use braces to prevent |
| 582 | greedy token parsing. You may also use double-quoted strings if the |
| 583 | string contains single quotes, so you do not have to use escape |
| 584 | characters. |
| 585 | |
Derek Jones | 129c181 | 2011-10-05 17:15:44 -0500 | [diff] [blame] | 586 | **INCORRECT**:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 587 | |
Derek Jones | 129c181 | 2011-10-05 17:15:44 -0500 | [diff] [blame] | 588 | "My String" // no variable parsing, so no use for double quotes |
| 589 | "My string $foo" // needs braces |
| 590 | 'SELECT foo FROM bar WHERE baz = \'bag\'' // ugly |
| 591 | |
| 592 | **CORRECT**:: |
| 593 | |
| 594 | 'My String' |
| 595 | "My string {$foo}" |
| 596 | "SELECT foo FROM bar WHERE baz = 'bag'" |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 597 | |
| 598 | SQL Queries |
| 599 | =========== |
| 600 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 601 | SQL keywords are always capitalized: SELECT, INSERT, UPDATE, WHERE, |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 602 | AS, JOIN, ON, IN, etc. |
| 603 | |
| 604 | Break up long queries into multiple lines for legibility, preferably |
| 605 | breaking for each clause. |
| 606 | |
Derek Jones | 129c181 | 2011-10-05 17:15:44 -0500 | [diff] [blame] | 607 | **INCORRECT**:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 608 | |
Derek Jones | 129c181 | 2011-10-05 17:15:44 -0500 | [diff] [blame] | 609 | // keywords are lowercase and query is too long for |
| 610 | // a single line (... indicates continuation of line) |
| 611 | $query = $this->db->query("select foo, bar, baz, foofoo, foobar as raboof, foobaz from exp_pre_email_addresses |
| 612 | ...where foo != 'oof' and baz != 'zab' order by foobaz limit 5, 100"); |
| 613 | |
| 614 | **CORRECT**:: |
| 615 | |
| 616 | $query = $this->db->query("SELECT foo, bar, baz, foofoo, foobar AS raboof, foobaz |
| 617 | FROM exp_pre_email_addresses |
| 618 | WHERE foo != 'oof' |
| 619 | AND baz != 'zab' |
| 620 | ORDER BY foobaz |
| 621 | LIMIT 5, 100"); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 622 | |
| 623 | Default Function Arguments |
| 624 | ========================== |
| 625 | |
| 626 | Whenever appropriate, provide function argument defaults, which helps |
| 627 | prevent PHP errors with mistaken calls and provides common fallback |
| 628 | values which can save a few lines of code. Example:: |
| 629 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 630 | function foo($bar = '', $baz = FALSE) |