1.2.842. No Self Referencing Constant¶
It is not possible to use a constant to define itself in a class. It yields a fatal error at runtime.
The PHP error reads : Cannot declare `self <https://www.php.net/manual/en/language.oop5.paamayim-nekudotayim.php>`_-referencing constant 'self\:\:C2'
. Unlike PHP which is self-referencing, self referencing variables can’t have a value : just don’t use that.
The code may access an already declared constant with self or with its class name.
This error is not detected by linting. It is only detected at instantiation time : if the class is not used, it won’t appear.
<?php
class a {
const C1 = 1; // fully defined constant
const C2 = self::C2; // self referencing constant
const C3 = a::C3 + 2; // self referencing constant
}
?>
1.2.842.1. Connex PHP features¶
1.2.842.1.1. Suggestions¶
Give a literal value to this constant
Give a constant value to this constant : other class constants or constant are allowed here.
1.2.842.1.2. Specs¶
Short name |
Classes/NoSelfReferencingConstant |
Rulesets |
All, Analyze, Changed Behavior, Class Review, LintButWontExec |
Exakat since |
0.8.4 |
PHP Version |
All |
Severity |
Minor |
Time To Fix |
Quick (30 mins) |
Precision |
Very high |
Note |
This issue may lint but will not run |
Available in |