Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Instance usage of Class constant in object-oriented of PHP

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "PHP object-oriented instance usage of class constants", in daily operation, I believe many people in PHP object-oriented instance usage of class constants have doubts, Xiaobian consulted all kinds of information, sorted out simple and easy to use operation methods, hope to answer "PHP object-oriented instance usage of class constants" doubts helpful! Next, please follow the small series to learn together!

Class constant is a very important concept in PHP object-oriented programming. A firm grasp of class constant helps to further improve the level of PHP object-oriented programming. This article describes the use of class constants in PHP programming as an example. The details are as follows:

Class Constant: Class in which constant data is stored during the running cycle.

Definition:

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 qualifiers

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 attributes, non-static attributes, static methods, and non-static methods.

Note here:

$this represents the current object, so does it always represent the object of the class in which $this is located?

The answer is no! Because the value of $this does not depend on the class in which $this is located, but on the execution object (execution environment) when the method in which $this is located is called

The execution environment of the method. The $this in the method indicates which object the current method is executed in.

At this point, the study of "instance usage of class constants in PHP object-oriented" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report