The variable members of a class are called "attributes", "fields", and "features". in this document, they are collectively referred to as "attributes ". Attribute declaration starts with the keyword public, protected, or private, and then starts with a variable. The variables in the attribute can be initialized. today, when a constant of a class is declared and assigned a value, an error is made.
Example:
define('ONE', '111'); class test{const TWO = ONE . '222'; public $ab = ONE . '222'; function pp(){echo self::TWO;}}
The above statement is actually not feasible in syntax, and the browser will report an error:
Parse error: syntax error, unexpected '.'
I checked the php manual and explained how to assign values to class attributes and class constants:
Explanation of class attributes:
The variable members of a class are called "attributes", "fields", and "features". in this document, they are collectively referred to as "attributes ". Attribute declaration starts with the keyword public, protected, or private, and then starts with a variable. The variables in the property can be initialized, but the initialization value must be a constant. the constant here means that the php script is a constant during compilation, instead of the constant calculated in the running phase after the compilation phase.
Explanation of class constants:
The constant value must be a fixed value and cannot be a variable, class attribute, or result of other operations (such as function call.
We can see that the class attribute and class constant values must be fixed values during initialization, that is, they cannot be values after processing.
Example of declaring class attributes in the php manual:
In fact, if you really need to obtain the processed result when assigning values to class variables, you can put it in the constructor for processing.