Class Constants in PHP | Object Oriented Concepts

Constants are a bit different than regular variables in PHP. Class Constants are immutable properties. Therefore, you cannot change the value after defining it for the first time. In this article, we will discuss Class Constants in PHP.

What are Class Constants in PHP?

It is possible to define Class Constants in PHP. A constant is an immutable class property. You can only define the value of a constant once as it remains unchangeable after that. There is no need to use the $ symbol for accessing them.

It is important to note that Class Constants in PHP must be an expression, and not a property or a function call. Also, a constant name should not be a reserved keyword (static, parent, self etc).

Class Constants in PHP are allocated once per class, and not for every instance.

When Should You Use Class Constants?

You must be thinking about deciding to choose when to use Class Constants. Here are some recommendations when you should chose Class Constants over regular variables:

  • If you think a particular value will never change during processing, you should define it as a constant.
  • You should define common properties like Database and Table names as constants.
  • Always avoid hard-coding. Hard-coding is mentioning a value directly in the code. What if you need to change that value after some time? You might have to search in multiple files if you have a large code-base. Constants will come in very handy in this case.

Defining a Class Constant in PHP

You can define a Class Constant in PHP using the const keyword.

<?php
	class TestClass {
		CONST DB_NAME = 'concatly';
		CONST PORT = 123;
	}
?>

The above code snippet defines two class constants in the class TestClass.

Accessing Constants Inside The Class

You can easily access constants using the self keyword within the class.

<?php
	class TestClass {
		CONST DB_NAME = 'concatly';
		CONST PORT = 123;

		public function printConstants() {
			echo self::DB_NAME;
			echo self::PORT;
		}
	}
	$testObject = new TestClass();
	$testObject->printConstants();
        /*
        concatly123
        */
?>

Accessing Constants Outside The Class

Class Constants in PHP can be accessed outside the class using the Class Name along with a static call to the constant.

<?php
	class TestClass {
		CONST DB_NAME = 'concatly';
	}
	echo TestClass::DB_NAME;
	/*  concatly  */
?>

Visibility of Class Constants

Class Constants in PHP are public by default. Therefore, you can access any constant outside the class by statically accessing the constant with the class name.

As of PHP 7.1, it is possible to define visibility access with Class Constants.

<?php
	class TestClass {
		public CONST DB_NAME = 'concatly';
		private CONST PORT = 123;
	}
	echo TestClass::DB_NAME;
	/*  concatly  */
	
	echo TestClass::PORT;
	/*  Fatal error: Uncaught Error: Cannot access private const TestClass::PORT  */
?>

As shown in the above example, Fatal Error will be thrown if a private constant is accessed outside the class. You may read more about Access Modifiers on the other post.

Array Constants in PHP

Also, as of PHP 7.1, it is possible to have an array as a constant. This can be defined simply as the following:

<?php
	class TestClass {
		CONST TEST_ARRAY = array(
			'Ross', 'Rachael', 'Chandler'
		);
	}
	print_r(TestClass::TEST_ARRAY);
	/*
	Array
	(
	    [0] => Ross
	    [1] => Rachael
	    [2] => Chandler
	)
	*/
?>

Conclusion

Class Constants are very useful in defining properties that will remain unchanged. We discussed Class Constants in this article. You can also go through other articles on PHP.

Also, you may go through the PHP Official Documentation to know more about Class Constants.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *