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