Question

Will Zend Framework 2 continue to place underscores in front of private members and methods of classes? e.g:

class blah {
    private $_name;

    private function _doSomethingToName()
    {
    }
}

I see that they are still there in the dev release of ZF2.

Was it helpful?

Solution

Probably yes, accodring to the poll:

There are currently 381 responses:

  • 57% vote "Yes" (to remove the underscores)
  • 38% vote "No" (to retain underscores)
  • 4% vote "No opinion"

OTHER TIPS

From zf2 coding standard

Functions and Methods

Function names MUST contain only alphanumeric characters. Underscores are not permitted. Numbers are permitted in function names but are discouraged. ... For methods on objects that are declared with the "private" or "protected" modifier, the first character of the method name MAY be an underscore. This is the only acceptable application of an underscore in a method name, and is discouraged (as it makes refactoring to public visibility more difficult). Methods declared "public" SHOULD NOT contain an underscore.

http://framework.zend.com/wiki/display/ZFDEV2/Coding+Standards

Yes, and why shouldn't they? It's a convention to do so.

This is a convention in other object-oriented languages too.

Check out these two code convention guidelines:

But all and all this should be a team convention, which is probably how it is for Zend. I believe these code conventions should be used for clarification and consistency when you write code. For me, the Zend use of underscore prefix for private members is a great time saver, especially when I'm not very familiar with a class.

I was recently wondering about this question and did a search through the entire ZendFramework-2.1.4 source tree to get a definitive answer. The underscore convention appears to no longer be in use for variables. There is not even one private or protected variable declaration in the entire project that begins with a single underscore. There are a small number of variables in the following three files that begin with two underscores but that's it.

\ZendFramework-2.1.4\library\Zend\View\Renderer\PhpRenderer.php \ZendFramework-2.1.4\library\Zend\View\Renderer\ConsoleRenderer.php \ZendFramework-2.1.4\library\Zend\Stdlib\AbstractOptions.php

At least one of these files contains the following comment:

Note: all private variables in this class are prefixed with "__". This is to * mark them as part of the internal implementation, and thus prevent conflict * with variables injected into the renderer.

Regarding methods I was able to find three files that use "private function _" and 89 files that use "protected function _" to declare methods. There were also three files using "private static function _" and seven files using "protected static function _". These are a very small minority of the total number of method declarations in the project.

It would appear that this convention is dead or dying within Zend Framework 2.

EDIT: It looks like these two sections of the ZF2 codign standards state this clearly.

http://framework.zend.com/wiki/display/ZFDEV2/Coding+Standards#CodingStandards-Variables http://framework.zend.com/wiki/display/ZFDEV2/Coding+Standards#CodingStandards-FunctionsandMethods

Variables

Variable names MUST contain only alphanumeric characters. Underscores are not permitted. Numbers are permitted in variable names but are discouraged in most cases.

For variables that are declared with private or protected visibility, the first character of the variable name MAY be a single underscore. This is the only acceptable application of an underscore in a variable name, and is discouraged (as it makes refactoring to public visibility more difficult).

Functions and Methods

Function names MUST contain only alphanumeric characters. Underscores are not permitted. Numbers are permitted in function names but are discouraged.

Function names MUST always start with a lowercase letter. When a function name consists of more than one word, the first letter of each new word MUST be capitalized. This is commonly called "camelCase" formatting.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top