# PHP编码规范

# OMS目前已针对PHP5.6版本做兼容,在写法上面需要注意下面几个点:

# 引用的使用

实例化类的对象时底层已经使用引用方式,所以不用再在类的实例化的时候增加引用符号

错误的写法:

$orderObj = &app::get('ome')->model('orders');

正确的写法:

$orderObj = app::get('ome')->model('orders');
1
2
3
4
5
6
7

方法定义的参数为引用方式,实际调用方法时参数还写着引用符号

错误的写法:

function test(&$a){

}

$this->test(&$a);

正确的写法:

function test(&$a){

}

$this->test($a);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# 参数的调用

参数名使用全局变量将会导致一个致命错误

错误的写法:
function foo($_GET, $_POST){

}
1
2
3
4

# 方法的调用

定义的方法是一个共有方法,调用的时候不能使用静态方法的方式调用

错误的写法:

function test(){

}

self::test();

正确的写法:

static public function test(){

}

self::test();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# 类的继承,方法重写

子类继承父类的方法,方法命名的参数与保持一致

错误的写法:

class a{
    function test($param1,&$param2){

    }
}

class b extends class a{
    function test(){
    
    }
}

正确的写法:

class a{
    function test($param1,&$param2){

    }
}

class b extends class a{
    function test($param1,&$param2){
    
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

# 方法套方法

写代码一时的便捷,方法套方法的使用

错误的写法:
kernel::single('base_xml')->xml2array(file_get_contents(ROOT_DIR.'/config/deploy.xml'),'base_deploy');

正确的写法:
$deploy = file_get_contents(ROOT_DIR.'/config/deploy.xml');
kernel::single('base_xml')->xml2array($deploy,'base_deploy');
1
2
3
4
5
6

# 更加详细的PHP版本升迁变更请查看:

  1. 从 PHP 5.3.X 迁移到 PHP 5.4.X
  2. 从 PHP 5.4.x 迁移到 PHP 5.5.x
  3. 从PHP 5.5.x 移植到 PHP 5.6.x
最后更新: 8/10/2018, 5:49:47 PM