Home > Article > Backend Development > Php object-oriented – class constants_PHP tutorial
Php object-oriented – class constants
Class constants: In the class, data that remains unchanged during the running cycle 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 = ‘female’;
}
Class constants are not restricted by access qualification modifiers
Visit:
Class::Constant name
Example:
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.
Note: $this represents the current object, does it always represent the object of the class where $this belongs?
No, because the value of $this does not depend on the class where $this is located, but depends on the execution object (execution environment) when the method where $this is located is called
The execution environment of the method, the environment of the object in which the current method is executed,
$this represents which object.