Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

PHP 

Do you follow the PSR Standards

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
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;
    }
} 
(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.
[Image: GwdtvQ8.png]
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.
I agree with MineSQL. I too prefer that "way" of coding as it i find it easier and the code is more tidy!
I prefer his first way to be honest, it's easier to read and a lot more "fluent" in my eyes.
I only use https://ricochet.im - ricochet:lmi3tdtlsnziqwdk

Jabber: [email protected]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[The Brotherhood] by VinavilMar 27, 2026 09:05 AM 25 860 Mar 27, 2026 09:05 AM
Last Post: Vinavil
[PHP] by SIM-1947090Aug 13, 2016 11:41 AM 0 206 Aug 13, 2016 11:41 AM
Last Post: SIM-1947090
[PHP] by IdolizeSep 26, 2015 04:59 AM 3 156 Sep 26, 2015 04:59 AM
Last Post: c00perzz