Get the App
SLTechnology News&Howtos  ›  Development  › 

Instance usage of Class constant in object-oriented of PHP

Shulou Source: shulou.com Published: 2022-06-03 19:04:40 09月19日 Update

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!

Tags: Constants objects methods instances static learning location environment programs programming design examples depending on properties more help practical solid important and then Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information MariaDB Apple Huawei Xiaomi