How to format a variable block in PHP?

I just want to know which formatting method I should prefer if I need to declare a variable block.

I have seen the alignment style several times:

$foo    = 'foo';
$bar    = 'bar';
$foobar = 'foobar';
$baz    = 'baz';

and assignment style:

$foo = 'foo';
$bar = 'bar';
$foobar = 'foobar';
$baz = 'baz';

Is there a standardized way or an official guide for this? I have already looked for this in PSR-1 and PSR-2 but it seems that these standards are not coping with this issue.

And if there is no official answer, how do other languages ​​do it and why do they prefer how they do it?

+4
source share
4 answers

. , , , "". , .

, , :

$foobiedoobie     = 'foo';
$barquax    = 'bar';
$foob = 'foobar';
$boob        = 'baz';

... - . : , , " " ( , ), ( ).

, , , .

, :

$foo = 'foo';
$bar = 'bar';
$foobar = 'foobar';
$baz = 'baz';

... - , , . , /.

, , , , , ( ).

+3

, ,

- , :

$vari = 5;              # i => integer
$vara = array("hello"); # a => array
$vars = "hello";        # s => string
// ...

, , :)

+1

. ( Zend Framework):

    $this->_mode             = $this->_detectMode();
    $this->_homeDirectory    = $this->_detectHomeDirectory();
    $this->_storageDirectory = $this->_detectStorageDirectory();
    $this->_configFile       = $this->_detectConfigFile();

:

/**
 * @var string
 */
protected $_storageDirectory = null;
/**
 * @var string
 */
protected $_configFile = null;
0

PSR does not say a word about this, so there is no single agreement at all. This is a matter of personal preference or company coding standards.

The alignment style, as you called it, may look better, but not every IDE can format such code (using the built-in refactoring functions), so it can annoy some people because the IDE format function “broke” the agreement when you format the whole file using a shortcut.

0
source

Source: https://habr.com/ru/post/1617037/


All Articles