Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP5.6 are arrays allowed in CLASS constants

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?

like image 851
Hao Chang Avatar asked Aug 11 '16 15:08

Hao Chang


People also ask

Can we define array as constant?

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.

Can an array be a constant PHP?

PHP Constant Arrays In PHP7, you can create an Array constant using the define() function.

What is a class constant?

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.


1 Answers

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);
like image 102
Whiteulver Avatar answered Sep 18 '22 16:09

Whiteulver