Skip to content

Instantly share code, notes, and snippets.

@RyanNielson
Last active August 29, 2015 14:03
Show Gist options
  • Save RyanNielson/8ba885502a0c67a659cd to your computer and use it in GitHub Desktop.
Save RyanNielson/8ba885502a0c67a659cd to your computer and use it in GitHub Desktop.
PHP Styleguide

These are some guidelines I came up with that we should follow to ensure consistency and readability in our codebase. This guide is a combination of other PHP style guides, Laravel expectations, and preferences. Things like function length and line length aren't required, just sometimes good to follow. Most of these rules can be setup in your editor so you don't have to worry about them.

Coding Style

  • Use soft-tabs with a 4 space indent.
  • Try to keep lines 100 characters long or less.
  • Try to keep functions 10-20 lines long. Don't be afraid to break them into smaller functions, it makes code easier to understand most of the time.
  • Remove all trailing whitespace on lines.
  • End each file with a blank newline.
  • Use spaces around operators, after commas.
$sum = 1 + 1
1 > 2 ? true : false
[1, 2, 3]
  • No spaces after (, [ or before ], ).
  • No spaces after !
!$this->isTruthy()
  • Use one empty line between functions
function function1() 
{
    return true;
}

function function2() 
{
    return false;
}

Syntax

  • Always use { and } for while, for, and if statements, even if they are only one line.
  • Always place { and } on the line after the function, while, for, if, etc.
if (true) 
{
    echo "blah";
}
  • An exception is when passing closures or anonymous functions you should place the { and } on the same line as the definition.
Mail::send("view/name", function() {
    echo "neato";
});
  • Always use === and !== when comparing values as it prevents confusing errors.
if ("cat" === "dog") 
{
    echo "that doesn't make any sense.";
}
  • Use the new array operator [] over the old array() when creating a new array.
// Good
$array = ["cat", "dog"];      

// Bad
$array = array("cat", "dog");

Naming

  • Use camelCase for variable and function names.
  • Use PascalCase for class names.
  • Use SCREAMING_SNAKE_CASE for constants.
  • Avoid making instance variables in classes public, instead make get and set methods matching the variable name and make the instance variable private.
class TestClass 
{
    private $testVar = "Test";
    
    public function getTestVar() 
    {
        return $this->testVar;
    }
    
    public function setTestVar($value) 
    {
        $this->testVar = $value;
    }
}

Comments

  • Comment code to describe why you did something, not exactly what it's doing.
  • Code should be understandable and not need comments for every line. Variable naming and function naming should be obvious. Don't be afraid to use longer variable and function names if they are more descriptive.
// Good
$player1 = "Ryan";
$player2 = "Patrick";

// Bad
$p1 = "Ryan";
$p2 = "Patrick";
  • Use comment blocks before functions, even if they're very simple. It helps keeps code organized and we can easily create API documentation in the future.
/**
 * Returns the users name for some reason.
 *
 * @param  string  name
 * @return string
 */
function usersName(string $name) 
{
	return $name;
}

Strings

  • Prefer string interpolation or string concatenation:
// Good
$name = "{$user->getFirstName()} {$user->getLastName()}";

// Bad
$name = $user->getFirstName() . " " . $user->getLastName();
  • Prefer single-quoted strings when not doing interpolation, it's easier to type and looks better.
// Good
$name = 'Ryan';

// Bad
$name = "Ryan";

Files

  • Try to limit a file to a single class if it contains a class definition.
  • A files name should be the same as the class defined within it, including the same capitalization.
// This should be in SiteUser.php
class SiteUser 
{
    // ...
}
  • If a file isn't defining a class, and it is a standard PHP script, name it using snake_case and make the name whatever the functions within relate to.

Editor settings

In your editor of choice to should set the following settings so keep things consistant.

  • Tab size: 4 spaces
  • Trim whitespace on save
  • Add newline to end of file automatically
@PLaRoche
Copy link

PLaRoche commented Nov 6, 2014

Can you add your sublime config you have that ensures the editor settings?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment