code_style.md 767 Bytes
Newer Older
1 2 3
Yii2 core code style
====================

Alexander Makarov committed
4 5 6 7

Proposals
---------

8 9
### Brackets

Alexander Makarov committed
10 11 12
It's better to be consistent with brackets not to remember where should we use
newline and where not:

13 14 15 16 17
~~~
class MyClass
{
	public function myClassMethod()
	{
Qiang Xue committed
18
		if($x) {
19
			// do it
Qiang Xue committed
20
		} else {
21 22 23 24 25 26
			// some code
		}
	}
}
~~~

Alexander Makarov committed
27
Use brackets even for one line `if`s.
28

Qiang Xue committed
29 30 31 32
> I chose to use the style as shown in Component.php because I want to make the
> curly brackets consistent with array brackets regarding newlines. Similar coding
> style is also used in Symfony 2.

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
### Use type hinting like

~~~
public function __construct(CDbConnection $connection)
{
	$this->connection = $connection;
}
~~~

instead of

~~~
public function __construct($connection)
{
	$this->connection = $connection;
}
~~~