This article mainly introduces the PHP object-oriented programming and other constant usage, is a very important concept of PHP object-oriented programming, for PHP beginners are necessary to be firmly mastered, the need for friends can refer to the next
Class constants are a very important concept in PHP object-oriented programming, and a firm mastery of class constants can help further improve the level of PHP object-oriented programming. In this paper, we describe the usage of class constants in PHP programming in the form of examples. Specific as follows:
class constants: A class that holds unchanged data for the duration of a run .
Defined:
const keyword Const constant NAME = constant value
Examples are as follows:
Class Student{public $stu _id;public $stu _name;public $stu _gender;const gender_male= ' male '; const Gender_female = ' female ';}
Class constants are not restricted by access-qualified modifiers
Access method:
Class:: Constant Name
Examples are as follows:
Class Student{public $stu _id;public $stu _name;public $stu _gender;const gender_male= ' male '; const Gender_female = ' female '; 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 a class are: constants, static properties, non-static properties, static methods, non-static methods .
Here's what to note:
$this represents the current object, will he always represent the object of the $this class?
The answer is NO! Because the value of $this does not depend on the class in which the $this resides, it depends on the execution object when the $this method is called (execution Environment)
The execution environment of the method in which the current method executes, and the $this within the method represents which object.