Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
This is a more controversial recommendation, but has many benefits for OOP code. It lets everyone know where they should put various bits of syntax, which helps avoid all sorts of obscure problems with version control and code contributions.
This is a more controversial
recommendation than PSL1,
0:00
which is widely used and adopted.
0:03
PSL2 is used widely too, but
0:04
has a few parts that some developers are
happier about more than others.
0:07
The reasoning here, is that PSL2 is a
style guide and
0:11
each developer has their own unique
preferences when it comes to style.
0:14
Regardless of the personal preferences of
a developer,
0:17
using PSL2 has many benefits for your
code.
0:20
And as everyone conformed to a single code
style guide, which helps avoid all sorts
0:23
of obscure problems with version control
and co-contribution from third parties.
0:26
Its also a great set of common rules to
follow when you work on a team.
0:31
To stop any potential arguments that might
pop up about where a bracket should go.
0:34
You can just do what PSL 2 does.
0:37
I'm gonna take you through the rules in
the overview.
0:40
One at a time and explain what they mean.
0:42
Code must follow PSR1.
0:44
To be PSR2 compliant, you also have to
follow the rules in PSR1.
0:45
PSR2 adds on top of these rules.
0:50
Code must use four spaces for indenting
not tabs.
0:53
This seems counter-intuitive to many.
0:56
The tabs versus spaces wars have been
going on forever and
0:58
will never end, but the important thing is
consistency.
1:01
The more projects use the same thing, the
less IDE config switching you need to do.
1:04
PSR2 pick spaces, so that's what we'll do.
1:08
Any good editor, can be set to enter up to
four spaces when you hit
1:11
the Tab key on your keyboard, so this
isn't a problem.
1:14
There must not be a hard limit on line
length.
1:18
The soft limit must be 120 characters, and
lines should be 80 characters or less.
1:21
This just means it would be cool if you
kept your code at less than 80 characters,
1:27
but that's not always possible.
1:30
It probably shouldn't be more than 120
either but if it is,
1:32
just give a warning not an error.
1:35
There must be one blank line after the
name-space declaration.
1:37
And there must be one blank line after the
block of use declarations.
1:41
This just helps things look pretty,
1:45
by adding some white space around the
namespace declaration.
1:46
Opening braces for classes must go on the
next line, and
1:50
closing braces must go on the next line
after the body.
1:53
That just means put curly brackets for
classes on their own lines.
1:56
Opening braces for the methods, must go on
the next line, and
2:00
closing braces, must go on the next line
after the body.
2:03
Put curly brackets for methods on their
own lines too.
2:06
Visibility must be declared on all
properties and methods.
2:09
Abstract and final, must be declared
before the visibility, and
2:12
static must be declared after the
visibility.
2:16
This sounds a little confusing.
2:19
But if you're creating an abstract or a
final class which is public,
2:20
the word abstract or final, should come
before public.
2:23
If you're using static classes, and
2:27
that method is public, then you should
write public before you write static.
2:28
You might not have learned about abstract
final or static classes yet.
2:31
These are all fairly advanced features.
2:35
So if you're unsure what they are, just
ignore this rule for now.
2:37
Control structure keywords must have one
space after them, and method and
2:40
function calls must not.
2:44
Control structure keywords are things like
if, else, switch, do, while and for each.
2:45
You need a space after these words, and
before the opening bracket.
2:51
Opening braces for control structures,
must go on the same line.
2:55
And closing braces, must go on the next
line after the body.
2:59
This means things like if, for each and
while, need to have their curly bracket on
3:02
the same line as the keyword, instead of
dropping it down a line.
3:06
Opening parentheses for
3:10
control structures, must not have a space
after them.
3:11
And closing parentheses for control
structures, must not have a space before.
3:14
That means no white space after the
opening bracket, and
3:18
no white space before the closing brace.
3:20
It can be quite hard to understand what
all of this means.
3:23
To help us, we can install a tool called
PHP_CodeSniffer.
3:26
This will allow you to test your code
matches against the standard.
3:29
Let's hop over to workspaces to see how
CodeSniffer works.
3:32
If we want to start working with
PHP_CodeSniffer,
3:36
the first thing we should do is install
it.
3:38
As with many things, we can use the
concert on here and type, composer choir.
3:40
The name of the vendor is Squizlabs.
3:47
The name of the package, PHP_CodeSniffer.
3:50
And once again, this is a dev dependency.
3:54
We are only using this for development and
not production.
3:56
It's gonna ask us to pick a version
constraint.
3:59
Once again, we don't really care.
4:01
So there you can see Composer doing the
work.
4:02
It's installed CodeSniffer, and now it's
there.
4:04
Now, just like with PHPUnit, we can run
vendor.
4:08
Bin, this is where our code lives.
4:13
And phpcs, is the executable for
CodeSniffer.
4:16
Then we pass SRC as an argument,
4:20
as that is the only directory we want to
sniff right now.
4:23
We can leave our tests and vendor folders
alone.
4:25
Next, we have to specify the standard
option.
4:28
Two hyphens.
4:33
And we want to pass PSR2 as the value to
that,
4:34
because that's the name of our standard
that we want to test against.
4:36
If you run this, CodeSniffer should tell
us anything that it finds that's
4:40
not PSR2 complying in any of the PHP files
that exist inside the source folder.
4:43
Lets have a look at what it's found.
4:49
Okay, here we go, it's found errors
inside, source example.php.
4:50
Cuz it has, that's the only file that we
have.
4:58
And there's quite a few errors here.
5:01
These numbers here, are line numbers.
5:03
This is the severity of the error.
5:05
Warnings you can kind of ignore.
5:07
Errors are a problem, and
5:09
it has a human readable message explaining
what's actually going wrong.
5:10
Let's have a look at example.php and try
and fix it.
5:14
So, there must be one blank line after the
namespace declaration.
5:17
That makes sense.
5:21
It also probably shouldn't be jammed up
there either,
5:22
must be one blank line after the you
statement.
5:24
Zero found.
5:26
Okay.
5:27
We can put a line in.
5:28
Nice and easy.
5:29
Let's try and shrink this list of errors
down a little bit.
5:31
Okay.
What happening now?
5:37
Line six, that's here.
5:38
Opening brace of a class must be on the
line after definition.
5:40
Oh, okay.
Well the definition line is right here,
5:44
and then we have a space and a bracket.
5:47
PSR2 says that it should be underlined by
itself.
5:49
Run again.
5:52
Excellent.
We're starting to get rid of some of
5:54
these errors now.
5:55
Class constants must be uppercased.
5:56
Expected FOO, but found lowercase foo.
5:58
Easy fix.
6:02
Don't forget to change any references to
this constant as well.
6:03
Expected function abc, but
6:07
found function abc with a space in between
the first parenthesis.
6:09
So we don't have a function called abc.
6:14
This is just a template error.
6:15
What they're really trying to say is,
6:17
that on line ten which is right here,
there shouldn't be a space here.
6:19
So you can get rid of that.
6:22
Get rid of a few more of these errors.
6:25
Starting to get much more manageable now.
6:26
So line ten, still problems with this
line.
6:31
What's wrong?
6:34
Well, we need to put the bracket, opening
brace it calls it, down here.
6:35
Line 14 is our next error.
6:41
The line numbers have changed, so let's
update this.
6:43
Okay 15, there's a lot of problems on line
15.
6:47
We have four different errors with this
one piece of code.
6:49
So let's take a look.
6:52
The static declaration must come after the
visibility declaration.
6:53
Visibility declaration is public, private
or protected.
6:58
So it means static, needs to go, there.
7:01
Once again, we have the same message,
function abc bracket.
7:05
Not found, found abc space bracket.
7:09
So that's easily fixed.
7:11
Okay, getting there.
7:14
15, still more problems with 15.
7:17
This is a very controversial line.
7:19
Expected zero spaces between the opening
bracket.
7:21
An argument bar, one found.
7:24
Expected zero spaces between argument bar
and closing bracket, one found.
7:26
That means, it expected zero spaces here
but found one, delete that space.
7:30
It expected, zero spaces there, but found
one, so delete that.
7:36
And if we look down here it says, opening
briefs should be on a new line.
7:41
So, that's simple.
7:44
Punt that down a line.
7:46
Go to the white space.
7:47
Let's run this again.
7:50
Okay.
7:53
White space found at end of line.
7:53
There's a few extra spaces there.
7:57
Get rid of that.
7:59
Inline control structures are not allowed.
8:00
Now if you're familiar, control structure
is something like if, do, while.
8:03
And, inline means that it's all happening
on the same line.
8:07
PHP allows you to do an inline control
statement if there's only one
8:11
statement happening.
8:14
The problem is, if somebody does something
like, then that
8:15
doesn't happen inside the IF.
8:21
That happens outside of the IF, which is
very confusing.
8:22
So, to avoid any potential confusion, you
just don't use inline control structures
8:26
[BLANK_AUDIO]
8:31
Okay.
There's still problems with line 17.
8:35
Expected zero spaces and found one.
8:38
Get rid of that, and that.
8:39
Keep on running CodeSniffer.
8:43
And so we haven't got any errors left.
8:44
True, false, and no, must be lower cased.
8:46
Expected false but found capital F false.
8:49
So, someone used a weird style there.
8:52
Let's change that.
8:54
And you'll see that now PHP recognizes
that as a, as a keyword instead.
8:55
On line, 18 we can see that its not been
indented correctly,
9:00
because there is only two spaces.
9:02
They should be four.
9:04
Line 21.
9:06
Gotta get rid of these spaces.
9:06
Also on 21 true, false and null must be
lowercased.
9:10
Again, someone's used caps lock instead,
so we can change that.
9:12
Let's run CodeSniffer again.
9:17
It all fits on one screen now.
9:19
We're getting there.
9:20
Expected one space after the function key
word.
9:22
Which line is this?
9:25
30.
9:26
Okay, so
9:27
when using a function when using an
enormous function need to put a space in.
9:29
Also on 30, closing breaks must be on a
line by itself.
9:33
Okay.
9:37
One new line at the end of a file.
9:39
Oh, we'll get to that in a second.
9:40
Each PHP statement must be in a line by
itself.
9:42
We've still got a problem there.
9:45
Very close to having this complete now.
9:48
Expected one new line at the end of the
file, found zero.
9:50
Okay?
9:53
There's an empty line.
9:54
The closing briefs for the class must go
on the next line after the body.
9:56
What that means, is that the closing
bracket for
9:59
the class, this, must be at the end of the
body.
10:02
That's here.
10:05
So, delete that line.
10:06
Perfect!
10:10
All the errors are gone.
10:10
Many IDs and code editors will either have
code sniffer built in.
10:13
Or available as a plug-in.
10:16
Using interactive tools like CodeSniffer,
should really help you learn about PSR2.
10:18
You don't always need to follow PSR2 in
your projects.
10:21
But it's slowly becoming far more common
for people to do so.
10:25
You need to sign up for Treehouse in order to download course files.
Sign up