Hack Forums
[PHP] Do you follow the PSR Standards - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Web Development (https://hackforums.net/forumdisplay.php?fid=183)
+---- Thread: [PHP] Do you follow the PSR Standards (/showthread.php?tid=4609498)



Do you follow the PSR Standards - iGeekOut - Dec 29, 2014

I've been doing PHP just over 3 weeks now and recently moved onto learning OOP and Design Patterns like MVC because when I do something I like doing it right, so I googled and found phptherightway.com
which lead me to php-fig.org so I decided to follow them and have already started to implement the best practices in projects.

So I was wondering if anyone else follows the PSR standards, and if so do you have any tips?

What I use:

Sublime Text 3
iTerm2
Vagrant LAMP Stack


* - MineSQL - Dec 29, 2014

I tend to pick and choose which guidelines to follow and which not to. I never had bad habits when I started coding, so the only thing that annoys me is the standard of braces on new lines for classes, methods, and functions.

example:
PHP Code

<?php 
//"the right way"
class Action
{

    public 
$lawsuit = "money";

    public function 
GetLawsuit()
    {
      return 
$this->lawsuit;
    }

} 

This just looks better to me
PHP Code

<?php 
class Action {

    public 
$lawsuit = "money";

    public function 
GetLawsuit() {
      return 
$this->lawsuit;
    }
} 



* - base - Dec 29, 2014

(Dec 29, 2014 07:23 PM)MineSQL Wrote:  I tend to pick and choose which guidelines to follow and which not to. I never had bad habits when I started coding, so the only thing that annoys me is the standard of braces on new lines for classes, methods, and functions.

example:
PHP Code

<?php 
//"the right way"
class Action
{

    public 
$lawsuit = "money";

    public function 
GetLawsuit()
    {
      return 
$this->lawsuit;
    }

} 

This just looks better to me
PHP Code

<?php 
class Action {

    public 
$lawsuit = "money";

    public function 
GetLawsuit() {
      return 
$this->lawsuit;
    }
} 

As someone whose been coding PHP for a few years, I also prefer the latter, however when working on OSS or in a group/team project it's best to adhere to the project standards.


* - iGeekOut - Dec 29, 2014

I don't mind it but that's because I am new, but if you've been coding a while you tend to pick up your own style and can be hard to change, but what I can't argue with is having an space for things like foreach and if, which adds to the readability.

PHP Code

<?php 
$names 
= ['Nicholas Cage', 'Adam Sandler', 'Liam Neeson'];

/* PSR-2 Compliant */
foreach ($names as $name) {
    
// loop
}

/* Not PSR-2 Compliant */
foreach($names as $name) {
    
// loop
} 

But if you're stuck in writing your code without complying to PSR-2 but want to, you can simply run PHP_CodeSniffer which will check your code, and in some cases fix most of your mistakes automatically and tell you the lines of the others which it can't. I use the command line version but there are plugins available.


* - c00perzz - Dec 30, 2014

I agree with MineSQL. I too prefer that "way" of coding as it i find it easier and the code is more tidy!


* - stackoverflowin - Dec 30, 2014

I prefer his first way to be honest, it's easier to read and a lot more "fluent" in my eyes.