PHP Class Constant Array

PHP Class Constant and PHP Constant array

There are two types of constants in PHP, the PHP constant array and the PHP class constant array. The constants can be defined pretty much anywhere using the define construct, while the php class constant array are defined within the individual class or interface using the const keyword.

While we cannot say that one type of constant is more important than the other, PHP 5.6 made the difference between the two by allowing PHP class constant with the array data type. Aside from that difference, both types of constants supported scalar values (integer, float, string, Boolean, or null).

PHP Class Constant Exemple

The PHP 7 Class Constant Array release addressed this inequality by adding the array data type to constants as well, making the following into valid expressions:

// The class constant - using 'const' keyword
class Rift {
const APP = [
'name' => 'Rift',
'edition' => 'Community',
'version' => '2.1.2',
'licence' => 'OSL'
];
}
// The class constant - using 'const' key
interface IRift {
const APP = [
'name' => 'Rift',
'edition' => 'Community',
'version' => '2.1.2',
'licence' => 'OSL'
];
}
// The constant - using 'define' construct
define('APP', [
'name' => 'Rift',
'edition' => 'Community',
'version' => '2.1.2',
'licence' => 'OSL'
]);
echo Rift::APP['version'];
echo IRift::APP['version'];
echo APP['version'];

Though having constants with the array data type might not be an exciting type of feature, it adds a certain flavor to the overall constant use.

keywords:

php constant class,php const array in class,php const,get class constants php,php constant array,php constant variable in class.