Class constants provide a mechanism for holding fixed values in a program. That is, they provide a way of giving a name (and associated compile-time checking) to a value like 3.14
or "Apple"
. Class constants can only be defined with the const
keyword - the define function cannot be used in this context.
As an example, it may be convenient to have a shorthand representation for the value of π throughout a program. A class with const
values provides a simple way to hold such values.
class MathValues {
const PI = M_PI;
const PHI = 1.61803;
}
$area = MathValues::PI * $radius * $radius;
Class constants may be accessed by using the double colon operator (so-called the scope resolution operator) on a class, much like static variables. Unlike static variables, however, class constants have their values fixed at compile time and cannot be reassigned to (e.g. MathValues::PI = 7
would produce a fatal error).
Class constants are also useful for defining things internal to a class that might need changing later (but do not change frequently enough to warrant storing in, say, a database). We can reference this internally using the self
scope resolutor (which works in both instanced and static implementations)
class Labor {
/** How long, in hours, does it take to build the item? */
const LABOR_UNITS = 0.26;
/** How much are we paying employees per hour? */
const LABOR_COST = 12.75;
public function getLaborCost($number_units) {
return (self::LABOR_UNITS * self::LABOR_COST) * $number_units;
}
}
Class constants can only contain scalar values in versions < 5.6
As of PHP 5.6 we can use expressions with constants, meaning math statements and strings with concatenation are acceptable constants
class Labor {
/** How much are we paying employees per hour? Hourly wages * hours taken to make */
const LABOR_COSTS = 12.75 * 0.26;
public function getLaborCost($number_units) {
return self::LABOR_COSTS * $number_units;
}
}
As of PHP 7.0, constants declared with define
may now contain arrays.
define("BAZ", array('baz'));
Class constants are useful for more than just storing mathematical concepts. For example, if preparing a pie, it might be convenient to have a single Pie
class capable of taking different kinds of fruit.
class Pie {
protected $fruit;
public function __construct($fruit) {
$this->fruit = $fruit;
}
}
We can then use the Pie
class like so
$pie = new Pie("strawberry");
The problem that arises here is, when instantiating the Pie
class, no guidance is provided as to the acceptable values. For example, when making a “boysenberry” pie, it might be misspelled “boisenberry”. Furthermore, we might not support a plum pie. Instead, it would be useful to have a list of acceptable fruit types already defined somewhere it would make sense to look for them. Say a class named Fruit
:
class Fruit {
const APPLE = "apple";
const STRAWBERRY = "strawberry";
const BOYSENBERRY = "boysenberry";
}
$pie = new Pie(Fruit::STRAWBERRY);
Listing the acceptable values as class constants provides a valuable hint as to the acceptable values which a method accepts. It also ensures that misspellings cannot make it past the compiler. While new Pie('aple')
and new Pie('apple')
are both acceptable to the compiler, new Pie(Fruit::APLE)
will produce a compiler error.
Finally, using class constants means that the actual value of the constant may be modified in a single place, and any code using the constant automatically has the effects of the modification.
Whilst the most common method to access a class constant is MyClass::CONSTANT_NAME
, it may also be accessed by: