2. 1. Error Suppression
- @ operator not allowed
- error_reporting(E_STRICT); should be defined
in code for dev & qa platforms (not prod!)
3. 2. global keyword
PHP allows to define vars as global. If it's done
in function it breaks function scope. Globals
are not allowed.
4. 3. Depricated features
Don't use depricated features in your code
PHP 5.3 - http://php.net/manual/en/migration53.deprecated.php
PHP 5.4 - http://php.net/manual/en/migration54.deprecated.php
PHP 5.5 - http://php.net/manual/en/migration55.deprecated.php
PHP 5.6 - http://php.net/manual/en/migration56.deprecated.php
5. 4. include_once() & require_once()
- Use these function when they are extremelly required
- Not allowed to use them inside of class methods
- Use autoloading instead
6. 5. Redundant Code
If($var == “true”){
return true;
} else{
return false;
} <==== NOT OK. Use following instead
return ($var == “true”) ? true: false; <== BUT
don't make it nested!!
7. 6. Redundant Booleans
Actually this looks bad also
return ($isBad) ? true : false;
it's the same as
return $isBad;
8. 7. Accidental Assignment
if($a = ”some value”) <== this will always give
“true” to avoid this you can use
if(“some value” == $a) – so if “=” is missed an
error will be produced
9. 8. Use type hinting
If your method takes an object as an argument
then type of the object should be defined:
public function doSomething($object) <== BAD
public function doSmth(Obj_Type $obj) <==
good
10. 9. Use === instead of ==.
- You can rely on variable type
- This will improve performance
- This also relates to other comparison operators
11. 10. Nested code blocks
- Only 2 nested code blocks allowed
if($a){
$x = 5;
if($b){
$x = 8;
if($c){
$s = 9;
}
}
} <== this is bad code. Declare new function to
resolve it
12. 11. Quering DB in loops
- Quering DB in loops is not allowed
- Use IN (X, Y, Z) instead
14. 13. Comments
- Comments should be added to each class
- Comments should be added to each method
and field
- If method does something complicated
comments may exist in method body
15. 14. Single function is doing only one
thing
- If you have function getUsers() it should just
return users but not delete them or return
some other things
16. 15. General code recommendations
- A method should not be larger than 40 lines of
code so function is viewable with out scrolling.
- Always initialize the variable before using it.
- No public class attributes.
- Don't query DB from view layer
- Always try using single quote ( ‘ ) when working
with the php string