If you want me to push this to Drupal core: base: coding standards please just say so, but I want a record keyed to Coder because I would like the rule changed in Coder (assuming it is changed in the standards).
Example:
/**
* Tags that may not have children (are self-closing).
*
* @see http://www.standardista.com/html5/xhtml-elements-their-parent-children-attributes-and-default-browser-presentation/
* @var array
*/
static public $TAGS_WITH_NO_CHILDREN = array(
'br',
'hr',
'img',
'base',
'link',
'meta',
'area',
'col',
'param'
);
Coder output:
ERROR | Class property $TAGS_WITH_NO_CHILDREN should use lowerCamel
| | naming without underscores
Using CAPITALS_WITH_UNDERSCORES for static class variables that act as constants is a common convention in many OO languages, especially Java. Examples:
- http://java.about.com/od/javasyntax/a/nameconventions.htm
- http://www.appperfect.com/support/java-coding-rules/namingconvention.html
Please consider at least permitting this variation for static class variables in the Drupal coding standards (and thus in Coder).
Comments
Comment #1
webel commentedThe Drupal JavaScript coding standards docs state:
I am not expressing my preference, I am just pointing out that it is a common convention, and should at least be permitted in PHP for static variables that act as constants.
If one does not use CAPITALS_WITH_UNDERSCORES the question remain how one can easily distinguish static from not-static class variables.
Comment #2
webel commentedMore interesting Java examples.
From Stackoverflow: http://stackoverflow.com/questions/1417190/should-a-static-final-logger-be-declared-in-upper-case:
Case:
private static final Logger logger = Logger.getLogger(MyClass.class);The logger reference is not a constant, but a final reference, and should NOT be in uppercase. A constant VALUE should be in uppercase.
The Java Coding Style Guide states this in paragraph 3.3 Field Naming
Following this convention, logger is a static final object reference as stated in point 2, but because it is followed by "." everytime you use it, it can not be considered as a constant and thus should be lower case.
Comment #3
klausiHm, in your example you are using a class property which is not a constant so the constant rules do not apply to it? Arrays cannot be declared as constants, so I guess you cannot use the "const" keyword. So this seems to be a special case - not sure what we should do about it?
Drupal 8 core uses lowerCamel naming for public static variables, example in core/lib/Drupal/Core/Language/Language.php. Also the public static $modules variable used in many test cases suggests that the rule "Methods and class properties should use lowerCamel naming." applies here.
So my interpretation is that this is the current standard and Coder behaves correctly according to it. So I'm closing this for now, please reopen if this changed through a core issue and in the coding standards.
Comment #4
webel commented@klausi wrote:
In some cases I am using (misappropriating) static class variables in PHP as constants that can be post-initialized (such as to apply Drupal t() translation to a shared string), noting that - unlike in Java - one can't apply final to static class variable in PHP, only to methods. If I use CAPITALS_WITH_UNDERSCORES it makes it clearer that they are acting like constants. One can't in this case use a const, because in PHP5.3 it can't be initialized with the result of a computation, and it can't be post-initialised. Please see new examples under my comment at Coder: This requirement is at odds with OO encapsulation: WARNING | Only string literals should be passed to t() where possible.
Comment #5
webel commentedReopened as support request, please close again on reading https://www.drupal.org/node/2306473#comment-9039965. Feedback welcome.
Comment #6
klausiResponded there!