Class constants are a very important concept in PHP object-oriented programming, and mastering class constants can help further improve the level of object-oriented programming in PHP. This paper describes the usage of class constants in PHP programming in the form of an instance. Specifically as follows:
class constants: In a class, save unchanged data in the running cycle .
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: the members that can be defined in a class are constants, static properties, Non-static properties, static methods, and Non-static methods .
Here you need to note:
$this represents the current object, does he always represent the object of $this's class?
The answer is NO! Because the value of the $this does not depend on the class in which the $this resides, it depends on the execution object (execution Environment) when the method is invoked $this.
The execution environment of the method, the current method is executed in the context of which object, and the $this in the method represents which object.