Friday, October 10, 2025
HomeLanguagesPHP Constant Class

PHP Constant Class

The const keyword is used to declare a class constant. A constant is unchangeable once it is declared. The constant class declared inside the class definition. The class constant is case-sensitive. However, it is recommended to name the constants in all uppercase letters. Constant differ from normal variables as no $ (dollar sign) is used. The default visibility of class constants is public. Class constants are useful when you need to declare some constant data (which does not change) within a class.

There are two ways to access class constant:

  1. Outside the Class: The class constant is accessed by using the class name followed by the scope resolution operator (::) followed by the constant name.

Example:

PHP




<?php
  
class code{
  
    // Declare const class
    const Greetings = "Welcome to neveropen";
}
  
// Access const class
echo code::Greetings
?>


Output:

Welcome to neveropen

2. Inside the Class: It can be accessed by using the self keyword followed by the scope resolution operator(::) followed by the constant name.

Example:

PHP




<?php
  
class code{
  
    // Declare const class
    const Greetings = "Welcome to neveropen";
            
    public function welcome(){
        echo self::Greetings;
    } 
}
  
// Access const class
$val = new code();
$val->welcome();
  
?>


Output:

Welcome to neveropen
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32349 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6717 POSTS0 COMMENTS
Nicole Veronica
11880 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6838 POSTS0 COMMENTS
Ted Musemwa
7097 POSTS0 COMMENTS
Thapelo Manthata
6792 POSTS0 COMMENTS
Umr Jansen
6792 POSTS0 COMMENTS