Home  >  Article  >  Backend Development  >  Php object-oriented – class constants_PHP tutorial

Php object-oriented – class constants_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:19:48935browse

Php object-oriented – class constants

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,

in the method

$this represents which object.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/871194.htmlTechArticlePhp object-oriented – class constant Php object-oriented – class constant class constant: in the class, saved within the run cycle, not changing data. Definition: const keyword const constant name = constant Example:...
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