How to define class constants in PHP (with code)

不言
Release: 2023-04-03 21:24:01
Original
5258 people have browsed it

The content of this article is about how PHP obtains the file name (code) through the path. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Definition of class constants

Constants defined in a class.

Class constants must be defined using the const keyword

const constant name = value;

The relationship between constants and objects

Constants cannot be accessed through objects

Access to class constants

##Class constants belong to the class, not to the object. Objects cannot access class constants.

Constant access must be implemented through classes:

Class name::Constant name

:: is called the range resolution operator

Classes are often used inside a class to access members of the class. When using a class to access members inside the class, there is a keyword similar to $this that represents the object: self, its own meaning, represents the current class name.

<?php
class person(){
    const HAOYUN=&#39;PHP软件开发工程师&#39;;
}
//类外部获取
$person=new person();
echo $person->HAOYUN;
echo HAOYUN;
echo person::HAOYUN;
//类内部获取
Copy after login

Related recommendations:

php class constant usage example analysis, php constant example analysis

php custom overall constants With class constants

The above is the detailed content of How to define class constants in PHP (with code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template