Php object-oriented-class constants, php object-oriented Constants
Php object-oriented-class Constants
Class constant: in the class, data that remains unchanged during the running period is saved.
Definition:
Const keyword
Const constant name = constant value
Example:
Class Student
{
Public $ stu_id;
Public $ stu_name;
Public $ stu_gender;
Const GENDER_MALE = 'male ';
Const GENDER_FEMALE = 'femal ';
}
Class constants are not restricted by access modifiers.
Access:
Class: constant name
Example:
Class Student
{
Public $ stu_id;
Public $ stu_name;
Public $ stu_gender;
Const GENDER_MALE = 'male ';
Const GENDER_FEMALE = 'femal ';
Public function _ construct ($ id, $ name, $ gender = '')
{
$ This-> stu_id = $ id;
$ This-> stu_name = $ name;
$ This-> gender = ($ gender = '')? Self: GENDER_MALE: $ gender;
}
}
Summary: Members that can be defined in the class, including constants, static attributes, non-static attributes, static methods, and non-static methods.
Note: $ this indicates the current object, always indicates the object of the class where $ this is located?
No, because the value of $ this does not depend on the class where $ this is located, but on the execution object (Execution Environment) when the $ this method is called)
The execution environment of the method, in which the current method is executed
$ This indicates the object.
How can a php Object obtain a constant outside the class?
Instantiate a class object, and then find the constant you want in the class through the class object. You can also use the class name to find a constant.
Why does php set up class constants?
The benefit of using a class constant is:
-No additional overhead for parsing during compilation
-The pooled tables are smaller, so internal search is faster.
-The class constant only exists in the specified "namespace", so the name of the miscellaneous is shorter.
-Code cleaner to make debugging easier