Two is better than one. Especially with PSR

You have probably heard before that what is inside counts. What they mean is that when you are looking, things like purpose and character is more important than the looks. Well … they are wrong. Not that those things do not matter because they do. Let’s get real though. You have never looked at anything and just loved the character or the behavior. At first, it is never what you think it is and you need to spend time to get to the truth for those things. What I am trying to say is that looks are first thing anyone takes notice of. Let’s figure out how to sharpen up shall we?

I am talking about your code style. I hope I did not lead you wrong. It needs to look good… Your code… Actually, it needs to look freaking great. First impression is everything. After you impress your onlookers, they can then awe upon the awesomeness of your clever code. That is the goal here. PHP-FIG people understand the importance of this so much so that they have given us two choices. I have said before in an earlier post (first PHP post actually) that PSR-1 and PSR-2 are for your styling but just stick with PSR-2. We are still going to go over the PSR-1 of course because we are good people and we do not leave a man behind. Also because of the fact that PSR-2 implements PSR-1.

PSR-1

PHP Tags

You can either use <?php ?> OR <?= ?> for your tags and nothing else. Moving on…

Encoding

Keep your PHP in UTF-8 encoded files without a BOM which is the byte order mark. Keep that out! Your IDE is most likely equipped to do this for you.

Objective

We have not really talked about objective PHP but things like traits, functions, classes, etc are your symbols. Your PHP file should either contain those or cause side effects like generate output, create or manipulate data, etc. It should not do both.

Autoloading

Follow the PSR-4 standard. We will discuss that bad boy in an upcoming post.

Class Names

Format is CamelCase. Of course first letter of the class name is capitalized.

Method Names

Format is the camelCase. Start your name with a lowercase letter and camelCase your way out.

Constant Names

No surprise here. All uppercase (just like the time you argued on YouTube over a video) and you may use underscores since you can not camelCase it.


That is all for PSR-1 folks. It was not so bad was it. You are probably already doing some of it. Lets take a look at the more strict PSR-2.

PSR-2

If PSR-1 is your cool uncle, then PSR-2 is your dad. More strict and less fun but you know he wants nothing but the best for you. If you just do what he says, even though some things that makes you shout WHYYYY and you do not understand it at the time, things will be just much more better. PSR-2 implements everything from PSR-1. Let’s see what it adds.

Indentation

This is where I shouted WHYYYY. It is spaces, not tabs… If you are already using spaces, by the way that’s just weird and probably so are you, then good for you. On the other hand, If you are using tabs, you know how in your editor it looks fine and you are glad you did not spend all that time pressing the space bar and make anyone near you listen to that, but when you open the file in some other IDE and code looks all over the place? That’s because you are using tabs. I hope the previous run off sentence can imply how furious I am about this. The definition of what “a tab” is may differ between IDEs. That is why the FIG people decided to go with spaces. That is why I call PSR-2 the dad and not the fun and cool uncle (and because of some additional things it recommends).

Files and lines

Ugggg I am still not over the indentation part. I will soldier on and keep it to myself because I like you and want you to learn.
First of all, get rid of the closing ?> tag at the end of your PHP file.
Use Unix linefeed(LF) endings.
Leave an extra empty line at the end of your file.
Keep your lines below 80 characters. If you must, you may go up to 120. If you still need more than that, think about the choices you are making.
Do not leave a trailing white space at the end of a line. We are counting by the character over here…


You may feel overwhelmed at this point. Do not worry. Just like riding a bike or if you are a driver in USA, just like not using your turn signals, it will become muscle memory.


Keywords

You know you sometimes think of a name for your variable and suddenly your IDE gives it a different color than your other variables and you are like wooaaaa… That is because you came across a PHP keyword. When it comes to actually using your keywords (true, false, null etc), keep them lowercased.

Namespaces

When you declare a namespace (and you should), give it a blank line under it.
If you are importing or aliasing a namespace with the use statement, group all that together and give it a blank line afterwards.


I realize at this point that this post has gotten quite long and I probably should have created a different post for PSR-2. I am sticking to my guts.


Class and Method declarations

Your brackets for your class or method declarations, both opening and closing, should reside on a new line.
If you need to declare what your class implements or extends, keep those statements on the same line with your class name.
When you pass parameter(s) to your methods, your opening parenthesis should not be followed by a trailing, and your closing parenthesis by a preceding space. By the way, this was another WHYYYY moment for me.
You must declare visibility for each class property and method. Do not omit the public keyword and always use the private and protected keywords whenever applicable.
If you need to add “abstract” or “final”, do it before the visibility keywords.
When using multiple parameters for your functions, separate them with a command and a trailing single white space.

Control Structures

When you need to use a control structure that needs a parenthesis, leave a space between the control structure and the opening parenthesis. Your opening bracket should reside on the same line, after the control structure keyword. Closing bracket must reside on a new line. Make sure your opening parenthesis is not followed and your trailing parenthesis is not preceded by a white space. I do not get this. I really do not. Why am I cramming everything that I may need to edit later? Let me give you an example.
if ($isComputerOn === true) { ... } This is the recommended way. Why ? I am never going to change the “if” keyword or turn the parenthesis into bracket. If I want to edit / remove the variable, according to the recommendations, I must land my mouse cursor exactly between the opening bracket and the dollar sign. *sigh* Wouldn’t be easier to keep the things I will not edit like the “if” or the “()” all crammed up next to each other instead of what may actually change? Here is how I like to write.
if( $isComputerOn === true ) { ... } That is way easier to read for me. But, as I’ve said, I am embracing the change. I will not expect the rest of the world to change to the way I like even if it is clearly the better way 🙂 By the way, I actually had to re-write the way I like because I kept writing it PSR-2 style. See? Muscle memory.

Posted in PHP