How to access const in PHP class?

PHP has different functions as a free and cross-platform scripting language. These include data encryption, control of user access, creation of dynamic page contents, form data collection, sending and receiving cookies, and modifying data in a database.

The best thing is that PHP freelancers and remote development teams constantly vouch for the benefits of PHP, and that's primarily because it runs on different platforms such as Windows, Linus, and Mac Os. It's also compatible with a lot of the servers used to create websites today.

One of the widely used features of PHP is the PHP const, especially in Class. But what do PHP const and class mean? This article will provide an in-depth insight into everything you should know about PHP Const in Class, and PHP in general.

What is a PHP Const?

Certain features are constant across different programming and scripting languages; some are classes, constants, and variables. Starting with variables, in PHP, they function as placeholders for storing information in the middle of a PHP program. The value of the variable is the value of the most recent assignment. Denoted with a leading dollar sign ($), there are eight types of variables, and one of them is Constants.

A Const, also known as a constant, in PHP is a variable that serves as an identifier for a simple value on a PHP script. Constant values have a special feature that makes them stand out compared to variables. You can never change constant values while executing the script while it runs. A PHP Const, however, follows the same rules as PHP variables.

How is a PHP Const defined?

Just like a PHP variable, a PHP Const has to be declared or defined to be run on the script. There are two major ways to define a PHP constant. The first one is using the define () function. You can use the define () to define constants at run time. The function runs like this:

define (name, value).

The “name” in this function is case-sensitive. So, you can only declare the name of a constant when you start with a capital letter or an underscore.

An example of this can be seen below:

The second way to define a PHP Constant is by using the const keyword. This is the more common method. The const keyword defines constant when it's compile time. As a result, it's not a function, just a PHP language construct. Every constant defined using the const keyword is case-sensitive.

An example of this is:

PHP Constants have multiple use cases in a script. One of the places you can use constants in PHP is in class declarations.

What is a PHP Class?

You must understand what PHP functions are to understand a class in PHP. Functions are blocks of statements that are used repeatedly in a program. Executed by calling the function, a PHP function provides code that takes an input as a parameter, processes it when called, and returns a value. A function name must start with a letter or an underscore. However, unlike constants, they're not case-sensitive. It serves as a blueprint and template for the objects and instances of the class."

A PHP Class, on the other hand, serves as a blueprint and template for the objects and instances of the class. A PHP class is object-oriented and used as a message form for functions and other objects. You can also use PHP Classes to describe entities with defined properties and behaviors. A class is a container of variables and functions which come together to form an object.

Sometimes, you need to define a constant that is specific to a class. In this case, you can use the PHP class constants.

To define a constant of a class, you use the const keyword. For example:

<?php class Circle { const PI = M_PI; }

Code language: Python (python)

In this example, we define the PI constant in the Circle class. By convention, a constant name is in uppercase. If the constant name contains multiple words, you can use the underscore (

<?php class Circle { const PI = M_PI; private $radius; public function __construct(float $radius) { $this->radius = $radius; } public function area(): float { return self::PI * $this->radius ** 2; } }

Code language: Python (python)
0) to separate the words, for example

<?php class Circle { const PI = M_PI; private $radius; public function __construct(float $radius) { $this->radius = $radius; } public function area(): float { return self::PI * $this->radius ** 2; } }

Code language: Python (python)
1.

Since a constant is defined per class, not per instance of the class, you use the

<?php class Circle { const PI = M_PI; private $radius; public function __construct(float $radius) { $this->radius = $radius; } public function area(): float { return self::PI * $this->radius ** 2; } }

Code language: Python (python)
2 keyword to reference the constant inside the class. For example:

<?php class Circle { const PI = M_PI; private $radius; public function __construct(float $radius) { $this->radius = $radius; } public function area(): float { return self::PI * $this->radius ** 2; } }

Code language: Python (python)

In this example, we define the Circle class with the

<?php class Circle { const PI = M_PI; private $radius; public function __construct(float $radius) { $this->radius = $radius; } public function area(): float { return self::PI * $this->radius ** 2; } }

Code language: Python (python)
4 property. Inside the

<?php class Circle { const PI = M_PI; private $radius; public function __construct(float $radius) { $this->radius = $radius; } public function area(): float { return self::PI * $this->radius ** 2; } }

Code language: Python (python)
5 method, we calculate the area of the circle using the radius and the

<?php class Circle { const PI = M_PI; private $radius; public function __construct(float $radius) { $this->radius = $radius; } public function area(): float { return self::PI * $this->radius ** 2; } }

Code language: Python (python)
6 constant.

When you define a constant in a class, its visibility is public by default. It means that you can also access the constant outside of the class.

To reference to the constant outside the class, you use the class name and

<?php class Circle { const PI = M_PI; private $radius; public function __construct(float $radius) { $this->radius = $radius; } public function area(): float { return self::PI * $this->radius ** 2; } }

Code language: Python (python)
7 operator like this:

<?php // Circle class // ... echo Circle::PI;

Code language: Python (python)

Note that you can reference the class using a variable with the value is the class name. For example:

<?php $className = 'Circle'; echo $className::PI;

Code language: Python (python)

In this example, instead of using

<?php class Circle { const PI = M_PI; private $radius; public function __construct(float $radius) { $this->radius = $radius; } public function area(): float { return self::PI * $this->radius ** 2; } }

Code language: Python (python)
8, we use the

<?php class Circle { const PI = M_PI; private $radius; public function __construct(float $radius) { $this->radius = $radius; } public function area(): float { return self::PI * $this->radius ** 2; } }

Code language: Python (python)
9 to reference the PI constant.

Since PHP 7.1.0, you can use visibility modifier keywords such as

<?php // Circle class // ... echo Circle::PI;

Code language: Python (python)
0,

<?php // Circle class // ... echo Circle::PI;

Code language: Python (python)
1, and

<?php // Circle class // ... echo Circle::PI;

Code language: Python (python)
2 with the class constant. For example:

<?php class Circle { private const PI = M_PI; // other methods }

Code language: Python (python)

In this example, the PI constant is private and cannot be used outside the Circle class.

It’s possible to define a class constant using a constant expression. A constant expression is an expression that contains only constants. For example:

<?php class MyClass { const ONE_THIRD = 1/3; }

Code language: Python (python)

PHP class constants and inheritance

The following example illustrates how to define a constant in the parent class and override it in the child class. For example:

<?php abstract class Model { protected const TABLE_NAME = ''; public static function all() { return 'SELECT * FROM ' . static::TABLE_NAME; } } class User extends Model { protected const TABLE_NAME = 'users'; } class Role extends Model { protected const TABLE_NAME = 'roles'; } echo User::all(); // SELECT * FROM users; echo Role::all(); // SELECT * FROM roles;

Code language: Python (python)

How it works.

First, define a constant

<?php // Circle class // ... echo Circle::PI;

Code language: Python (python)
5 in the

<?php // Circle class // ... echo Circle::PI;

Code language: Python (python)
6 class. In the

<?php // Circle class // ... echo Circle::PI;

Code language: Python (python)
7 static method, returns a query that selects all rows from the table name specified by the

<?php // Circle class // ... echo Circle::PI;

Code language: Python (python)
5 constant.

Second, define the

<?php // Circle class // ... echo Circle::PI;

Code language: Python (python)
9 and

<?php $className = 'Circle'; echo $className::PI;

Code language: Python (python)
0 classes that extend the

<?php // Circle class // ... echo Circle::PI;

Code language: Python (python)
6 class. In the User and Role class, redefine the

<?php // Circle class // ... echo Circle::PI;

Code language: Python (python)
5 constant.

Since the

<?php // Circle class // ... echo Circle::PI;

Code language: Python (python)
9 and

<?php $className = 'Circle'; echo $className::PI;

Code language: Python (python)
0 classes inherits the

<?php // Circle class // ... echo Circle::PI;

Code language: Python (python)
7 method of the

<?php // Circle class // ... echo Circle::PI;

Code language: Python (python)
6 class, they can call the

<?php // Circle class // ... echo Circle::PI;

Code language: Python (python)
7 method.

When the

<?php // Circle class // ... echo Circle::PI;

Code language: Python (python)
9 class calls the

<?php // Circle class // ... echo Circle::PI;

Code language: Python (python)
7 method, the

<?php // Circle class // ... echo Circle::PI;

Code language: Python (python)
7 method returns the expected

<?php class Circle { private const PI = M_PI; // other methods }

Code language: Python (python)
1 constant defined in the

<?php // Circle class // ... echo Circle::PI;

Code language: Python (python)
9 class. The same logic is applied to the

<?php $className = 'Circle'; echo $className::PI;

Code language: Python (python)
0 class.

How to use const in PHP?

To create a constant, use the define() function..
name: Specifies the name of the constant..
value: Specifies the value of the constant..
case-insensitive: Specifies whether the constant name should be case-insensitive. Default is false..

How to print const variable in PHP?

There's the constant() function for retrieving the value of a previously defined constant. Although, your code - as posted - should also print the constant's value. Double check if you haven't by accident defined() the constant (as opposed to define() d). Save this answer.

What is the default visibility of const in PHP?

The default visibility of class constants is public .

What are the best practices of constants in PHP?

Good practice is to put static parameters that might be changed later in constants at the top of the file, so that they could be found and easily changed, if necessary, instead of find/replacing everything. Another one: constant should always be in CAPITAL_LETTERS, so it would be clear that it is a constant.