PHP OOP Class Constants (2024)

PHP - Class Constants

Constants cannot be changed once it is declared.

Class constants can be useful if you need to define some constant data within a class.

A class constant is declared inside a class with the const keyword.

Class constants are case-sensitive. However, it is recommended to name the constants in all uppercase letters.

We can access a constant from outside the class by using the class name followed by the scope resolution operator (::) followed by the constant name, like here:

Example

<?php
class Goodbye {
const LEAVING_MESSAGE = "Thank you for visiting W3Schools.com!";
}

echo Goodbye::LEAVING_MESSAGE;
?>

Try it Yourself »

Or, we can access a constant from inside the class by using the self keyword followed by the scope resolution operator (::) followedby the constant name, like here:

Example

<?php
class Goodbye {
const LEAVING_MESSAGE = "Thank you for visiting W3Schools.com!";
public function byebye() {
echo self::LEAVING_MESSAGE;
}
}

$goodbye = new Goodbye();
$goodbye->byebye();
?>

Try it Yourself »



PHP OOP Class Constants (2024)

FAQs

How to define class constants in PHP? ›

Class Constants ¶
  1. Example #1 Defining and using a constant. class MyClass. { const CONSTANT = 'constant value'; ...
  2. Example #3 Class constant expression example. const ONE = 1; class foo { const TWO = ONE * 2; ...
  3. Example #4 Class constant visibility modifiers, as of PHP 7.1.0. class Foo { public const BAR = 'bar';

What is a constant class in OOP? ›

A class constant is declared inside a class with the const keyword. A constant cannot be changed once it is declared. Class constants are case-sensitive. However, it is recommended to name the constants in all uppercase letters.

How to access constant from another class in PHP? ›

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: Outside the Class: The class constant is accessed by using the class name followed by the scope resolution operator (::) followed by the constant name.

What is the naming convention of class constants in PHP? ›

Naming constants

Constants' names are always written in the UPPER case and multiple words in one name are usually separated by an underscore.

How do you make a class variable constant? ›

Class Variables and Methods

To make a class variable constant, add the keyword final as a modifier on the declaration. It's better to make your constants also static -- since the value won't change, it's more efficient to have one variable shared by the entire class.

How do you name a class with constants? ›

The names of variables declared class constants and of ANSI constants should be all uppercase with words separated by underscores ("_"). (ANSI constants should be avoided, for ease of debugging.)

Should class constants be static? ›

Yes, it should be static if the values never change.

What are constant functions in OOP? ›

Declaring a member function with the const keyword specifies that the function is a "read-only" function that doesn't modify the object for which it's called. A constant member function can't modify any non-static data members or call any member functions that aren't constant.

What are the differences between PHP constants and variables? ›

Usually, constants are written in numbers. On the other hand, variables are written in letters and symbols. PHP constants are automatically global across the entire script. PHP variables are not automatically global in the entire script.

What is a constant in PHP? ›

A constant is an identifier (name) for a simple value. The value cannot be changed during the script. A valid constant name starts with a letter or underscore (no $ sign before the constant name). Note: Unlike variables, constants are automatically global across the entire script.

How to check if constant is defined in class PHP? ›

PHP | defined() function

The PHP defined() function is an inbuilt function in PHP which checks whether a constant is exists or not, in other words, defined or not. Syntax: bool defined($constant_name);

How to check if a constant is defined in PHP? ›

defined(string $constant_name ): bool. Checks whether a constant with the given constant_name is defined. This function works also with class constants and enum cases. Note: If you want to see if a variable exists, use isset() as defined() only applies to constants.

What are the rules for class naming in PHP? ›

The class name can be any valid label, provided it is not a PHP reserved word. A valid class name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: ^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$ .

What is the scope of constant in PHP? ›

Like superglobals, the scope of a constant is global. Constants can be accessed from anywhere in a script without regard to scope. For more information on scope, read the manual section on variable scope. Note: As of PHP 7.1.

What is the rule for class name in PHP? ›

Class names should be descriptive nouns in PascalCase and as short as possible. Each word in the class name should start with a capital letter, without underscore delimiters. The class name should be prefixed with the name of the “parent set” (e.g. the name of the extension) if no namespaces are used.

How to define variable in class in PHP? ›

The var keyword in PHP is used to declare a property or variable of class which is public by default. The var keyword is same as public when declaring variables or property of a class.

How to define a class in PHP? ›

Basic class definitions begin with the keyword class , followed by a class name, followed by a pair of curly braces which enclose the definitions of the properties and methods belonging to the class. The class name can be any valid label, provided it is not a PHP reserved word.

Can we define classes in PHP? ›

A class is a definition of the properties and methods of a particular type of object. When a class is used to define a new object, the resultant object is referred to as an instance of that class. In a PHP class definition, properties are variables, methods are functions.

Top Articles
Latest Posts
Article information

Author: Reed Wilderman

Last Updated:

Views: 6161

Rating: 4.1 / 5 (72 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Reed Wilderman

Birthday: 1992-06-14

Address: 998 Estell Village, Lake Oscarberg, SD 48713-6877

Phone: +21813267449721

Job: Technology Engineer

Hobby: Swimming, Do it yourself, Beekeeping, Lapidary, Cosplaying, Hiking, Graffiti

Introduction: My name is Reed Wilderman, I am a faithful, bright, lucky, adventurous, lively, rich, vast person who loves writing and wants to share my knowledge and understanding with you.