简体   繁体   English

是否有有用的PHP命名约定?

[英]Are there helpful PHP naming conventions?

I come from Java / Objective-C to PHP and find it horrible . 我来自Java / Objective-C到PHP,发现它很糟糕 I mean, PHP is nice, really. 我的意思是,PHP很好,真的。 But when you look at a bunch of variables, you don't know: Is that an number? 但是当你看到一堆变量时,你不知道:这是一个数字吗? Is that an string? 那是一个字符串吗? Or ist that even a fancy object that can perform actions when calling methods on it? 或者甚至是一个花哨的对象,可以在调用方法时执行操作?

So I wonder if there are helpful re-usable naming conventions for variables to help figure out if something is an object, or if something is just a boring variable. 所以我想知道变量是否有有用的可重用命名约定来帮助确定某些东西是否是某个对象,或者某些东西只是一个无聊的变量。 I'd say if something is an object, ie an instance of a class, the first character must be BIG. 我会说如果某个东西是一个对象,即一个类的实例,那么第一个字符必须是BIG。 But it's just a guess. 但这只是猜测。 Hope to read some tips from PHP pros :-) 希望从PHP专业人员那里阅读一些技巧:-)

As in other languages, there is no single coding standard in PHP. 与其他语言一样,PHP中没有单一的编码标准。 You can, among others (see the comments), check out the Zend coding standards , they are quite highly regarded as they are very close to (and partly identical with) the PHP core development team. 除了其他(请参阅注释)之外,您还可以查看Zend编码标准 ,因为它们与PHP核心开发团队非常接近(并且部分相同)。

What's to prevent you from using the conventions you're already familiar with? 什么是阻止您使用您已经熟悉的约定? It's likely that there is nothing unique to PHP that you've not already witnessed before. 很可能PHP之前没有任何独特的东西,你以前没有见过。

I usually stick to this simple rule 我通常坚持这个简单的规则

Meaningfull Camel Case Variable names (with type prefix before) ex : intNbDaysLeft would be good for an integer 有意义的Camel Case变量名(前面带有类型前缀)ex:intNbDaysLeft对整数有用

I use Zend Eclipse, so I describe my variables like this, to make sure they're intellisensed. 我使用Zend Eclipse,因此我会像这样描述我的变量,以确保它们被智能化。 Then, I worry less about, "what is this?" 然后,我更少担心,“这是什么?”

/**
 * @var Zend_Form_Element
 */
protected $_member;

or 要么

/**
 * @param Zend_Form_Element $input
 */
function do_it( $input )

or 要么

$my_var = some_function(); /* @var $my_var Zend_Form_Element */

You should quickly discover that there is little type hinting in PHP, and the reason for that is clear, in this dynamic language type can change, so name is not very important. 您应该很快发现在PHP中几乎没有类型提示,其原因很明显,在这种动态语言类型中可能会发生变化,因此名称不是很重要。

Using PHPDoc with Eclipse or NetBeans helps, but you cant rely on that, if you want to be sure that Your variable is of specific type, you have to check it (is_array(), instanceof, ect.). 将PHPDoc与Eclipse或NetBeans一起使用有帮助,但是你不能依赖它,如果你想确定你的变量是特定类型的,你必须检查它(is_array(),instanceof,ect。)。

Also, PHP does a lot for you, it will convert betwen types on Your behalf, it will act differently depending on the type. 此外,PHP为您做了很多,它将代表您转换类型,它将根据类型采取不同的行动。

PHP mostly works with strings, so most variables are of that type. PHP主要使用字符串,因此大多数变量都属于该类型。 Applications can have lots of types, but a single method shouldn't use too many. 应用程序可以有很多类型,但单个方法不应该使用太多。 If you find yourself using so many objects that you cant easily keep track of their instance names, then it's some design problem, not naming convention issue. 如果您发现自己使用了很多对象而无法轻松跟踪其实例名称,那么这是一些设计问题,而不是命名约定问题。

If you want to be sure you have not used some variable wrongly, be sure to unit test. 如果你想确定你没有错误地使用某些变量,请务必进行单元测试。 The standard in PHP is phpunit PHP中的标准是phpunit

我最喜欢的PHP命名约定是“PHP”总是被称为“巨大的cr-我们不会用bargepole接触”的约定。

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

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