PHP Class Constants

Keeping Code Grounded in a Sea of Variables

Class constants are similar to regular constants with the exception that they live inside the class. Class constants cannot be changed, hence the name constant. They’re declared using the const keyword and are allocated in memory once per class and not each time that you instantiate the object; this is what’s meant when you hear that constants live within the class. They resemble static variables in that sense, but static variables can be modified. To access the class constant, you need to use the class name and the scope resolution operator (double colon).

Constants do not have any visibility modifiers, such as private, public, or protected, and they do not have the $ preceding them like the properties have. The quickest way to illustrate constants is with an example. Let’s build on to the Dog and Car classes that we created in the previous article.

PHP — P44: Class Properties

If we start with the Dog class, what are some constants that each Dog should have? In other words, what is something that doesn’t change between each dog? A couple of constants that pop into my head is that each dog has a tail and has a heart. So, let’s add those to the class. Remember, constants are added outside of the methods but inside of the class curly braces. They begin with the keyword const and are usually written in all caps to help differentiate them from all other keywords.

const HAS_HEART = true;
const HAS_TAIL = true;

<?php
// Class Constants

class Dog {

    const HAS_HEART = true;
    const HAS_TAIL = true;

    public $eye_color = "Brown";
    public $dob = "January 30, 2017";
    public $does_shed = true;
    public $kingdom = "Animalia";
    public $phylum = "Mammalia";
    public $class = "Mammalia";
    public $order = "Carnivara";
    public $family = "Canidae";
    public $genus = "Canis";
    public $species = "Canis Lupus";
    public $subspecies = "Canis Lupus Familiaris";
    public $breed = "German Shepherd Dog";
    public $fur_color = "Black and Tan";
}

If we want to quickly display the output of the constant, we don’t have to instantiate the object; we can use the resolution operator to access the constant value that’s located in the Dog class, i.e. Dog::HAS_HEART.

var_dump( Dog::HAS_HEART );

Let’s look at another example, the Car class. In order for a car to be street-legal, it must have headlights, taillights, and turn signals. These are the constants that we’ll define for our Car class.

Again, to define these, we start with the const keyword followed by the constant name. I would think that at this point it’s not necessary to mention the naming convention of a constant since constants follow the same naming convention as all other labels in PHP, but here it is: a valid constant name starts with a letter or an underscore, followed by any number of letters, numbers, or underscores.

const HAS_HEADLIGHTS = true;
const HAS_TAIL_LIGHTS = true;
const HAS_TURN_SIGNALS = true;

We also don’t have to just var_dump our constant values; we can use them to build a string. Let’s combine our constant value inside of a ternary operator to create a little string.

echo “This vehicle is “ . ((Car::HAS_HEADLIGHTS) ? “a car.”: “is not a car.”);

If we defined our constants properly, the output of the statement above should be: The vehicle is a car.

<?php

class Car {

    const HAS_HEADLIGHTS = true;
    const HAS_TAIL_LIGHTS = true;
    const HAS_TURN_SIGNALS = true;

    public string $color;
    public string $make;
    public string $model;
    public int $year;
    public int $fuel_type = 93;
    public int $hp;
    public int $tq;
    public string $transmission = "6 Speed Manual";
    public string $vehicle_type = "Coupe";
    public float $exterior_height;
    public float $exterior_width;
    public float $exterior_length;
    public string $exterior_um = "in";
    public float $weight;
    public string $weight_um = "lbs";
}

echo "This vehicle is " . ((Car::HAS_HEADLIGHTS) ? "a car.": "is not a car.");

I’ll leave you with one final thought on constants. Constants can store the same values as properties in a class. That’s all you have to know about constants. Have fun programming.

Continue your learning with these articles

PHP Class Properties

CODE REAL ESTATE: WHERE PROPERTIES DEFINE OBJECT VALUE

PHP – P44: CLASS PROPERTIES 

Properties, also called Class Member Variables, Attributes, and Fields, are the characteristics of a class. Imagine them as variables placed inside of the class’s curly braces, but not inside of methods themselves.

PHP Class Constants

Keeping Code Grounded in a Sea of Variables

PHP – P45: Class constants

Class constants cannot be changed, hence the name constant. They’re declared using the const keyword and are allocated in memory once per class.

PHP Methods

CONDUCTING CODE HARMONY THROUGH EXPERT METHODS

PHP – P46: METHODS 

What is a method? In PHP, it’s just a function inside of a class. That’s the easiest way to think about it.

Leave a Reply