PHP Object-oriented class constants usage examples

Source: Internet
Author: User
Tags php programming
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.