So we can define array constants in PHP5.6. However, I got the following error when defining an array constant inside a class
Arrays are not allowed in class constants
So is it true that array constants are allowed but not inside classes?
Arrays are Not Constants It defines a constant reference to an array. Because of this, we can still change the elements of a constant array.
PHP Constant Arrays In PHP7, you can create an Array constant using the define() function.
Class constants can be useful if you need to define some constant data within a class. A class constant is declared inside a class with the const keyword. Class constants are case-sensitive. However, it is recommended to name the constants in all uppercase letters.
As of PHP 5.6 arrays are allowed in class constants.
See the link for a working php code.
<?php
class Foo
{
const BAR = [1,2,3];
}
print_r(Foo::BAR);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With