Created
December 3, 2012 18:49
-
-
Save tmountain/4197051 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
We base our coding standards loosely off of the PHP Pear coding standards. The Pear coding standards | |
are formally documented here: http://pear.php.net/manual/en/standards.php. I will go over the main line items | |
we care about below for your convenience. | |
Indents should be four spaces: | |
<?php | |
class Foo | |
{ | |
public function bar() | |
{ | |
if ($condition) { | |
... | |
} | |
} | |
} | |
?> | |
Control structures: | |
1) Put spaces between parens, curly braces, and operators | |
and put the curly brace on the same line as the conditional. | |
<?php | |
if ($foo || $bar) { | |
... | |
} | |
?> | |
2) Always include curly braces for conditionals. | |
(avoid this) | |
<?php | |
if ($foo) | |
....; | |
?> | |
(and avoid this) | |
<?php | |
if ($foo) ... ; | |
?> | |
Function calls should look like this: | |
<?php | |
$var = foo($bar, $baz); // spaces between args, no space before the paren | |
?> | |
Class definitions should have the brace on a new line: | |
<?php | |
class Foo | |
{ | |
// code goes here | |
} | |
?> | |
Function definitions should look like this: | |
<?php | |
function fooFunction($arg1, $arg2 = '') | |
{ | |
if ($condition) { | |
... | |
} | |
return $val; | |
} | |
?> | |
Arrays should be aligned to make the key/value pairs easy to read: | |
<?php | |
$fooArray = array( | |
'foo' => 'bar', | |
'spam' => 'ham', // trailing comma is okay | |
); | |
?> | |
Separate logical code blocks by one line of whitespace: | |
(use your best judgement here...) | |
<?php | |
private function doSomething($var1, $var2) | |
{ | |
$this->load->model('foo_model'); | |
$this->load->model('bar_model'); | |
if ($var1 && $var2) { | |
// some code... | |
} | |
} | |
?> | |
Variables should use studly camel-case (first letter lowercase, then camel case after): | |
(avoid using underscores) | |
<?php | |
$fooBar; | |
$companyID; // etc... | |
?> | |
Models: | |
1) include _model in the filename (i.e., webex_model.php) | |
2) Camel case everything up to the underscore in the classname (i.e., class UserProfile_model) | |
Controllers: | |
1) filename is the lowercase name of the class (i.e., prospector.php) | |
2) the class would be: class Prospector in this case | |
Line lengths: | |
1) Try to keep lines 80 characters or less if possible (not a strict rule) | |
2) Wrap lines at operators when appropriate | |
<?php | |
if (isset($foo) && isset($foo['bar']) && isset($foo['baz']) && | |
isset($blah) ... ) { | |
} | |
?> | |
Avoid committing big blocks of commented code: | |
1) if a method is no longer required, don't comment it out, remove it. Git | |
will take care of preserving the legacy code for us. | |
Remove all debug code before pushing to the repository: | |
1) make sure no print_r() and echo() debug statements are present | |
2) git diff can help you here | |
Avoid using instance variables in controller methods, models: | |
1) generally unnecessary and limits reusability | |
2) it's okay to use them in libraries and support code when it is appropriate | |
Testing: | |
1) set your index.php file to the following define('ENVIRONMENT', 'development'); | |
2) avoid committing code that prints warnings to the screen | |
3) try to test all code paths before pushing to the master repository | |
4) check for error conditions that may occur (missing required fields, etc) | |
Validation: | |
1) Look at controllers/settings.php, saveCompany() for a good example of how | |
we handle form validation. | |
2) Validation should occur in the controller. Models should never read from | |
$this->input->post(...), $_POST, etc. Any data passed to a model should be | |
pre-sanitized so that we can assume it is clean. | |
(do this inside of the controller) | |
<?php | |
$query = $this->input->post('query', true); | |
$this->foo_model->get($query); | |
?> | |
(then the model method would be) | |
<?php | |
public function get($query) | |
{ | |
// assume $query is validated and do something with it | |
} | |
?> | |
Model methods should be structured like this: | |
(return a single result) | |
<?php | |
public function getByID($id) | |
{ | |
$sql = "SELECT id, companyProfileID, displayName, | |
.... | |
FROM userProfile | |
WHERE id = ?"; | |
$query = $this->db->query($sql, array($id)); | |
$result = $query->result_array(); | |
return $result ? reset($result) : $result; | |
} | |
?> | |
(return multiple results) | |
<?php | |
public function getAllUsers($companyProfileID) | |
{ | |
$sql = "SELECT id, companyProfileID, displayName, | |
... | |
FROM userProfile | |
WHERE companyProfileID = ?"; | |
$query = $this->db->query($sql, array($companyProfileID)); | |
if ($query->num_rows > 0) { | |
return $query->result_array(); | |
} else { | |
return array(); | |
} | |
} | |
?> | |
(return arrays instead of objects whenever possible) | |
(Note that we use the ? placeholder in both the queries above to avoid SQL injection. | |
This is absolutely required in all queries.) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment