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