以下代码可应用于Yii2.x核心库和官方扩展开发。如果你想拉请求代码到核心库中,建议使用此规范。我们不强制你使用此代码规范在你的应用中。选择你认为更舒服的方式即可。
你可以获取一份代码检测的配制:https://github.com/yiisoft/yii2-coding-standards
简单说,我们使用PSR-2兼容规范,所以应用于PSR-2的一切对我们的代码也同样适用。
PHP代码文件必须只能使用无BOM的UTF-8。
类名必须使用大驼峰式(首字母大写)声明。例如,Controller,Model。
这里的"类"涉及所有的类和接口。
/**
* Documentation
*/
class MyClass extends \yii\Object implements MyInterface
{
// code
}
类中常量必须使用全大写带下划线方式声明。例如:
<?php
class Foo
{
const VERSION = ‘1.0‘;
const DATE_APPROVED = ‘2012-06-01‘;
}
例如:
<?php
class Foo
{
public $publicProp;
protected $protectedProp;
private $_privateProp;
}
/**
* Documentation
*/
class Foo
{
/**
* Documentation
*/
public function bar()
{
// code
return $value;
}
}
参数,变量,属性和返回值必须声明类型,如boolean,integer,string,array或null。你也可以使用类名,像Model或ActiveRecord。如:对于一个数组类型可以使用ClassName[]。
改变已有的数据类型是不推荐的。除非你必须要写这样的代码。
public function save(Transaction $transaction, $argument2 = 100)
{
$transaction = new Connection; // bad
$argument2 = 200; // good
}
$str = ‘Like this.‘;
$str1 = "Hello $username!";
$str2 = "Hello {$username}!";
下面的方式是不允许的:
$str3 = "Hello ${username}!";
连接字符串时,使用点在空格周围。
$name = ‘Yii‘ . ‘ FrameWork‘;
长内容的字符串可以使用下面的方法:
$sql = "SELECT *"
. "FROM `post` "
. "WHERE `id` = 121 ";
对于数组,我们使用PHP 5.4中的短数组语法。
使用下面的方法来声明数组:
$arr = [3,14,15,‘Yii‘,‘FrameWork‘];
如果有太多的元素时,可单独分行:
$arr = [
3, 14, 15,
92, 6, $test,
‘Yii‘, ‘Framework‘,
];
使用下面的格式来关联数组:
$config = [
‘name‘ => ‘Yii‘,
‘options‘ => [‘usePHP‘ => true],
];
if ($event === null) {
return new Event();
}
if ($event instanceof CoolEvent) {
return $event->instance();
}
return null;
// the following is NOT allowed:
if (!$model && null === $event)
throw new Exception(‘test‘);
尽量避免当语句生效时,else在return之后。使用防卫条件。
$result = $this->getResult();
if (empty($result)) {
return true;
} else {
// process result
}
要优于:
$result = $this->getResult();
if (empty($result)) {
return true;
}
// process result
switch使用下面的格式:
switch ($this->phpType) {
case ‘string‘:
$a = (string) $value;
break;
case ‘integer‘:
case ‘int‘:
$a = (int) $value;
break;
case ‘boolean‘:
$a = (bool) $value;
break;
default:
$a = null;
}
doIt(2, 3);
doIt([‘a‘ => ‘b‘]);
doIt(‘a‘, [
‘a‘ => ‘b‘,
‘c‘ => ‘d‘,
]);
使用空格在function/use参数和语句之前:
// good
$n = 100;
$sum = array_reduce($numbers, function ($r, $x) use ($n) {
$this->doMagic();
$r += $x * $n;
return $r;
});
// bad
$n = 100;
$mul = array_reduce($numbers, function($r, $x) use($n) {
$this->doMagic();
$r *= $x * $n;
return $r;
});
<?php
/**
* Returns the errors for all attribute or a single attribute.
* @param string $attribute attribute name. Use null to retrieve errors for all attributes.
* @property array An array of errors for all attributes. Empty array is returned if no error.
* The result is a two-dimensional array. See [[getErrors()]] for detailed description.
* @return array errors for all attributes or the specified attribute. Empty array is returned if no error.
* Note that when returning errors for all attributes, the result is a two-dimensional array, like the following:
* ...
*/
public function getErrors($attribute = null)
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* Component is the base class that provides the *property*, *event* and *behavior* features.
*
* @include @yii/docs/base-Component.md
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class Component extends \yii\base\Object
/**
* Returns the list of attached event handlers for an event.
* You may manipulate the returned [[Vector]] object by adding or removing handlers.
* For example,
*
* ~~~
* $component->getEventHandlers($eventName)->insertAt(0, $eventHandler);
* ~~~
*
* @param string $name the event name
* @return Vector list of attached event handlers for the event
* @throws Exception if the event is not defined
*/
public function getEventHandlers($name)
{
if (!isset($this->_e[$name])) {
$this->_e[$name] = new Vector;
}
$this->ensureBehaviors();
return $this->_e[$name];
}
正如你上面例子中看到的,我们使用标签来格式phpDoc的内容。
下面是另外的语法用于类、方法和属性之间在文档中的连接:
为了给上面提到的连接其它的标签类或方法名,你可以使用下面的语法:
... as displayed in the [[header|header cell]].
当|之后是连接标签时,在|之前的是方法,属性或类相关。
当然也可以连接到引导,使用下面的代码:
[link to guide](guide:file-name.md)
[link to guide](guide:file-name.md#subsection)
尽可能使用empty()。
当条件嵌套比较混乱时,尽早返回。如果方法比较简短时,没有关系。
坚持使用static除非出现下面的场景:
属性可以通过配制组件来接收false,null,‘‘或[]值来不做一些事情。
原文:http://www.cnblogs.com/s1099312273/p/4330702.html