0

Let's say I want to use someClass only once per method. Is such style is PSR compatible?

class Foo
{

    public function myMethod($x)
    {
        // ... code ...

        $data = (new someClass())->getSomething($x);

        // ... code ...
    }

}
Peter
  • 16,453
  • 8
  • 51
  • 77
  • `php-cs-fixer` says that's psr-2 compatible; you can also use tools such as http://www.webcodesniffer.net/onlinecodesniffer.php – Federkun Nov 25 '16 at 17:30

1 Answers1

0

PSR-2, while dictating style for the sake of readability, doesn't really dictate whether or not what you're asking is acceptable, at least according to my understanding of it.

What I would say, though, is that if you're planning to invoke a method from someClass, what I would consider far more readable would be using the scope resolution operator, like so, which removes the requirement of instantiating the object using the 'new' keyword:

$data = someClass::getSomething($x);

This is functionally equivalent to your syntax, but more readable.

Karl Buys
  • 439
  • 5
  • 12
  • static methods are not equivalent to object methods at all. you don't invoke constructor, destructor and you can't use `$this` in such context – Peter Nov 28 '16 at 11:21