code_style.md 773 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 18 19 20 21 22 23 24 25 26 27 28 29
~~~
class MyClass
{
	public function myClassMethod()
	{
		if($x)
		{
			// do it
		}
		else
		{
			// some code
		}
	}
}
~~~

Alexander Makarov committed
30
Use brackets even for one line `if`s.
31

Qiang Xue committed
32 33 34 35
> 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.

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
### Use type hinting like

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

instead of

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