2017-07-22 46 views
3

我一直在学习PHP,看到人们如何命名的东西有很多变化。我热衷于至少与自己保持一致。什么时候应该在PHP命名中使用camelCase/Camel Case或下划线?

我应该在哪里使用骆驼案例,我应该在哪里使用下划线?

思考:

  • 变量/属性:$用户ID或$ USER_ID或$用户ID

  • 类:MyCustomClass或myCustomClass

  • 函数/方法:my_custom_function()或my_Custom_Function( )

任何思想感激。

回答

6

从PSR基本编码标准(https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-1-basic-coding-standard.md

类名必须在StudlyCaps声明。

类常量必须与下划线 隔板所有上壳体中声明。

方法名称务必在camelCase中声明。

本指南有意避免关于使用$ StudlyCaps,$驼峰,或$ under_score 属性名称的 任何建议

<?php 
namespace Vendor\Model; 

class Foo 
{ 
    const VERSION = '1.0'; 
    const DATE_APPROVED = '2012-06-01'; 

    private $StudlyCapsProp = null; 
    protected $camelCaseProp = null; 
    public $under_score_prop = null; 

    public function fooBar() 
    { 

    } 
} 
+1

谢谢。该指南继续指出:“本指南有意避免使用$ StudlyCaps,$ camelCase或$ under_score属性名称的任何建议。”以便澄清属性/变量。 –

+0

@ LinzDarlington是的,谢谢你,我会更新答案 –

相关问题