开发者

Using $CamelCase for object instances and $lower_case for everything else [closed]

开发者 https://www.devze.com 2023-02-10 11:34 出处:网络
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references,or expertise, but this question will likely solicit debate, a
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be imp开发者_开发问答roved and possibly reopened, visit the help center for guidance. Closed 11 years ago.

Here's a simple convention for naming variables:

Use $CamelCase for object instances and "$lower_case" for everything else.

Can you think of a reason not to use it?

I came up with this convention a few months ago and I've been using it in a few small projects since then. I'm about to start using it in a big project so I want to be sure I'm not missing something.


Use whatever naming convention that suites you best, as long as the project will only ever be developed on by you.

The reason we have standards is that when developers collaborate together on a project, there already used to the syntax and the naming conventions as the what we call the standard.

As your specifically coding in PHP then i would advise you to use a well known naming convention such as Zend or PEAR.

There is no reason what so ever to come up with a new naming convention unless all your programmers or people that work on the code all opt in for this and its thought out properly.

Some Conventions:

  • http://pear.php.net/manual/en/standards.naming.php
  • http://framework.zend.com/manual/en/coding-standard.naming-conventions.html

I Personally prefer Zend over PEAR but the above to are the most popular when it comes down to standards.

Edit: Some examples of the conventions i use.

  • Global Variables $MY_GLOBAL_VARIABLE
  • Generic Variables $lowerUpper
  • Class Names Upper_For_Each_Segment
  • Class Variables $this->lowerUpper
  • Class variables Private / Protected $this->_lowerUpper
  • Constants ALL_UPPER_CASE

You can modify the convention slightly to fit your requirements but its best to stick to the most popular convention at the time.


Usually one follows this age-old rules triple:

  • Use $ALL_CAPS for nominal constants.
  • Use $Each_Word_Capped for global variables.
  • Use $no_words_capped for local variables.

Notice how with this scheme, you always separate words with underscores: underscores serves as space characters to guide the eye. This is more valuable than you realize. This is a perfectly consistent rule, and much more readable than most alternatives. Try remove the spaces in this posting to see what I mean.

0

精彩评论

暂无评论...
验证码 换一张
取 消