'Is it necessary to put space after $this keyword in OOP PHP according to PSRs?
I studied PSR-2 and PSR-12 documentations completely but didn't find any explanation regarding whether to put any space after $this keyword.
My exact question is which one of following styles is more correct according to PSRs:
return $this->name;
or
return $this -> name;
sample codes to compare would be as below:
class Car
{
public $name;
public function getName()
{
return $this->name; // I've always seen this style (no spaces)
}
}
in comparison with:
class Car
{
public $name;
public function getName()
{
return $this -> name; // Includes spaces
}
}
Are both correct? If so, is there a reason to use one over the other?
Solution 1:[1]
There are no explicit rules for this in the standards mentioned, and no examples using $this in particular, but there are plenty of other examples of using the -> operator, and they consistently show it with no spaces around.
For instance, under "Method and Function Calls" (PSR-2 §4.6 / PSR-12 §4.7):
$foo->bar($arg1);
and
$foo->bar(
$longArgument,
$longerArgument,
$muchLongerArgument
);
This doesn't mean that the standard requires that style, but it's a small piece of evidence that this is what most PHP developers would naturally expect.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | IMSoP |
