Home  >  Article  >  Backend Development  >  Detailed explanation of usage examples of object-oriented class constants in PHP

Detailed explanation of usage examples of object-oriented class constants in PHP

伊谢尔伦
伊谢尔伦Original
2017-06-29 09:12:071153browse

This article mainly introduces PHP object-oriented programming and the like constants usage, which is a very important concept in PHP object-oriented programming, for PHP beginners It is even more necessary to have a firm grasp of it. Friends who need it can refer to

Class constants are a very important concept in PHP object-oriented programming. A firm grasp of class constants will help further improve the efficiency of PHP object-oriented programming. level. This article describes the usage of class constants in PHP programming in the form of examples. The details are as follows:

Class constants: In the class, data that remains unchanged during the running cycle is saved.

Definition:

const 关键字
const 常量名 = 常量值

Examples are as follows:

class Student
{
public $stu_id;
public $stu_name;
public $stu_gender;
const GENDER_MALE= '男';
const GENDER_FEMALE = '女';
}

Class constants are not restricted by access qualification modifiers
Access methods:
Class::Constant name

The example is as follows:

class Student
{
public $stu_id;
public $stu_name;
public $stu_gender;
const GENDER_MALE= '男';
const GENDER_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 the class are: constant, staticAttributes, non-static properties, static methods, non-static methods.

Note here:
$this represents the current object, so does it always represent the object of the class where $this belongs?
the answer is negative! 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, which object the current method is in When executed in an environment, $this in the method indicates which object.

The above is the detailed content of Detailed explanation of usage examples of object-oriented class constants in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn