Using Constants in Classes
In PHP, a constant is a type of variable whose value cannot be changed once it has been set. They're often used to store values that need to stay the same throughout the execution of a script, like a numerical constant (like pi, 3.14159...) or configuration values (like the version of your software).
When we talk about constants in classes, we mean values that are tied to a class, not an instance of the class (which we usually call an "object"). This is similar to a fact that is always true about a certain concept, regardless of the specific instances of it.
Defining a constant
Let's say we have a class called Circle
. All circles have something in common: the mathematical constant pi (π). So, we could store pi as a constant in our Circle
class:
class Circle {
public const PI = 3.14159;
// More code here...
}
We defined the constant PI
using the const
keyword. The name of constants is usually written in uppercase letters by convention.
Avoid the define function
Outside of classes, we can use the define()
function or const
keyword to define a constant. This is not true for classes. Only the const
keyword is supported. The define
function will cause an error.
Accessing a constant
To use a constant, we refer to it by the class name followed by two colons (::
) and the name of the constant. The ::
operator is known as the scope resolution operator.
Here's where things get interesting. We don't need to create an instance of the class to access the constant. We can directly type the class name followed by the constant name like so:
echo Circle::PI; // Outputs: 3.14159
Accessing Constants in methods
Accessing a constant inside a class method can be done using the self
keyword followed by the scope resolution operator (::
) and the constant name. Here's how you could do it:
class Circle {
const PI = 3.14159;
public function calculateArea($radius) {
return self::PI * $radius * $radius;
}
}
In this example, we've defined a constant PI
and a method calculateArea
. Inside calculateArea
, we access PI
using self::PI
.
Now, let's use this method:
$circle = new Circle();
echo $circle->calculateArea(5); // Outputs: 78.53975
In this case, we've created an object $circle
from the Circle
class, and we've called the method calculateArea
, which internally uses the constant PI
. The self::
keyword is used to access properties or methods from the same class, including constants.
Why not the $this keyword?
It's important to understand that constants don't require an instance of a class to be accessed. The $this
keyword always points to the current instance. Since an instance is not required for a constant, PHP introduces the self
keyword to allow constants to be accessed from within the class.
Key Takeaways
- Constants in a class are associated with the class itself, not instances (objects) of the class.
- Constants are defined using the
const
keyword inside the class, and their names are typically in uppercase. - Class constants can be accessed directly using the class name, followed by the scope resolution operator (
::
) and the constant's name, likeSomeClass::SOME_CONSTANT
. - Within the class, constants can be accessed using the
self
keyword followed by the scope resolution operator (::
), likeself::SOME_CONSTANT
.