2

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.

5 Answers 5

6

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"
2
  • Thanks for the stats takeshin. So I guess we just sit tight and see. Does anyone have any idea how far away a beta release is?
    – waigani
    Sep 21, 2010 at 20:35
  • @waigani Just clone the development repository and see how far away it is.
    – takeshin
    Sep 22, 2010 at 7:14
2

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

1

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

5
  • 4
    Why not? Its nearly useless and looks ugly :-) When you use an big framework like Zend you use an ide too, wich is able to show you private and protected functions with an nice image. Sep 20, 2010 at 4:18
  • 1
    @ArneRie well there is always that one time you dont have an ide and have to fix something. Its in my opinion very bad practice to code one way because of an IDE`s feature.
    – Iznogood
    Sep 20, 2010 at 4:53
  • 3
    @Iznogood than is there still the protected/public keyword in front of the function. Please give me one good reason to use an underscore! Sep 20, 2010 at 6:04
  • 4
    @ArneRie If you browse through some code within a method, you can directly see which other methods are private or public, without checking the method itself.
    – Tim
    Sep 20, 2010 at 10:47
  • 1
    @ArneRie What Tim said. And you know you do not have to follow these guidelines. If you want to use files1.php trhu files999.php and vars named hello123 thru hello999 you are free to do so. After a while you will see why conventions are a good thing.
    – Iznogood
    Sep 20, 2010 at 20:42
1

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.

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.