简体   繁体   English

CakePHP中的变量命名约定

[英]Variable naming conventions in CakePHP

What is the best way to name variables which contain multiple words? 命名包含多个单词的变量的最佳方法是什么? After going through lots of projects available in the CakePHP forge , I have noticed people use either camelCase, underscores or camelCase for variables and underscores for data sent to the view. 在完成CakePHP forge中的许多项目后,我注意到人们使用camelCase,下划线或camelCase来获取发送到视图的数据的变量和下划线。

An example of the last one would be: 最后一个例子是:

$activeSites = $this->Site->find('all',array('conditions'=>array('Site.active' => '1'), 'recursive' => -1));
$this->controller->set('active_sites', activeSites);

According to the naming conventions used for CakePHP itself ( http://book.cakephp.org/view/509/Coding-Standards#Variables-609 ), variables are named in the following way: 根据CakePHP本身使用的命名约定( http://book.cakephp.org/view/509/Coding-Standards#Variables-609 ),变量以下列方式命名:

Normal variables should start with a lowercase letter, and should be written in camelBack in case of multiple words. 正常变量应以小写字母开头,如果有多个单词,则应使用camelBack编写。

As most people will tell you, there is not "best way" to name variables, other than to be consistent. 正如大多数人会告诉你的那样,除了保持一致之外,没有“最好的方法”来命名变量。 Decide the naming convention you like the most, and stick to it. 确定您最喜欢的命名约定,并坚持下去。 If you're continuing on a project, keep the naming convention that is already there. 如果您继续执行项目,请保留已存在的命名约定。 That is all the advice I can give you. 这就是我能给你的所有建议。

There isn't a right or wrong answer to this. 对此没有正确或错误的答案。 I usually name it: 我通常把它命名为:

$active_sites = $this->Site->find('all',array('conditions'=>array('Site.active' => '1'), 'recursive' => -1));
$this->controller->set('active_sites', $active_sites);

I think any way is fine, but your example showed that the variable in the view and the controller isn't the same. 我认为任何方式都可以,但你的例子表明视图中的变量和控制器是不一样的。 That can be avoided by adopting $active_sites or $activeSites throughout. 通过采用$ active_sites或$ activeSites可以避免这种情况。

(Actually after a while, I start using underscores everywhere.) (实际上过了一段时间,我开始到处使用下划线。)

Cakephp的创始人使用camelCase风格

Variable names should be as descriptive as possible, but also as short as possible. 变量名称应尽可能具有描述性,但也应尽可能短。 Normal variables should start with a lowercase letter, and should be written in camelBack? 普通变量应该以小写字母开头,并且应该用camelBack编写? in case of multiple words. 在多个单词的情况下。 Variables containing objects should start with a capital letter, and in some way associate to the class the variable is an object of. 包含对象的变量应以大写字母开头,并以某种方式与该类关联,该变量是其对象。 Example: 例:

<?php  
     $user = 'John';
     $users = array('John', 'Hans', 'Arne');
     $Dispatcher = new Dispatcher();
?>

Usually you use underscores and only lower case/upper case for labels that are case insensitive. 通常,对于不区分大小写的标签,您使用下划线,仅使用小写/大写。 When passed around they may be handled in a case sensitive way. 传递时,可以以区分大小写的方式处理它们。

Case insensitive examples: 不区分大小写的示例:

  • GET/POST attributes - but on server side they may be handled in a case sensitive way GET / POST属性 - 但在服务器端,它们可能以区分大小写的方式处理
  • urls - but on server side they may be handled in a case sensitive way 网址 - 但在服务器端,它们可能以区分大小写的方式处理
  • filenames on windows - but when transferred to a *nix system they are case sensitive Windows上的文件名 - 但是当转移到* nix系统时,它们区分大小写

对于cakePHP应该是camelCase

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM