This article mainly introduces the constant usage of PHP object-oriented programming, which is a very important concept in PHP object-oriented programming. It is necessary for PHP beginners to master it, for more information, see
This article mainly introduces the constant usage of PHP object-oriented programming, which is a very important concept in PHP object-oriented programming. It is necessary for PHP beginners to master it, for more information, see
Class constants are a very important concept in PHP object-oriented programming. A strong grasp of class constants will help to further improve the level of PHP object-oriented programming. This article describes the usage of class constants in PHP programming as an example. The details are as follows:
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 Method:
Class: constant name
Example:
Class Student {public $ stu_id; public $ stu_name; public $ stu_gender; const GENDER_MALE = 'male'; const GENDER_FEMALE = 'feme'; public function _ construct ($ id, $ name, $ gender = '') {$ this-> stu_id = $ id; $ this-> stu_name = $ name; $ this-> gender = ($ gender = '')? Self: GENDER_MALE: $ gender ;}}
Summary: The members that can be defined in a class include constants, static attributes, non-static attributes, static methods, and non-static methods.
Note:
$ This indicates the current object. Does it always represent the object of the class where $ this is located?
The answer is 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 in the method indicates which object.