What are the advantages and disadvantages of declaring constants as an array?

11

I found this new feature a lot of way around PHP 5.6.

It is now possible to set a array to the value of a constant.

See how cool:

const BR_DATES = [
    1 => 'Janeiro',
    2 => 'Fevereiro',
    3 => 'Março',
    4 => 'Abril'
];


print_r(BR_DATES); // Array ( [1] => Janeiro [2] => Fevereiro [3] => Março [4] => Abril )


echo BR_DATES[date('n')]; // Março

I see that this implementation reduces pollution in the global scope in relation to the definition of numerous constants. In addition, it becomes very useful in reusing values that could be used in a fixed way, as in the case above.

In addition to the outstanding benefits, what are the other benefits that this new implementation will bring?

Would this implementation of the constant accept an array be the same as the Python Tuple ?

Does this implementation have any bad effects over previous versions?

    
asked by anonymous 03.03.2015 / 15:06

2 answers

6

Truth since version 5.6 of PHP is possible as constants array.

From my point of view and from the experience I have I would say that it is one more useful feature that comes from approaching PHP to some of the reality already present in other programming languages.

From this version 5.6 it is already possible:

const SEC_PER_DAY = 60 * 60 * 24; 

So, a novelty but just that. However and regarding the question ...

  

This implementation brings some harm to the versions   previous?

It can be problematic in certain environments where we do not control the version where our PHP applications will run, because in fact an array constant will be unacceptable in previous versions. For this purpose there is another way ...

define('MESES', serialize(array('Janeiro', 'Fevereiro' ...)));

This will always be compatible with earlier versions of PHP.

This is a recent solution and as we all know for certain production environments it is not good to go through major changes soon. Therefore it commands the prudence and the special experience that is used and in the immediate study of future implementations, but for production with some ... very ... care. Today and through an administrative panel is so easy to change the version of PHP.

    
03.03.2015 / 23:55
-1

It's almost always good to have more options, this makes the language more versatile. If the class had constants for months, it might be better to use something like the SplEnum example implementation, that is, a constant for each month.

link

<?php
class Month extends SplEnum {
    const __default = self::January;

    const January = 1;
    const February = 2;
    const March = 3;
    const April = 4;
    const May = 5;
    const June = 6;
    const July = 7;
    const August = 8;
    const September = 9;
    const October = 10;
    const November = 11;
    const December = 12;
}

echo new Month(Month::June) . PHP_EOL;

try {
    new Month(13);
} catch (UnexpectedValueException $uve) {
    echo $uve->getMessage() . PHP_EOL;
}

Well, if you compare if a month you received is January, you use a constant and not a typo-prone string.

About Python tuples, so I read that the Scala lists are similar, they are immutable, any modifications you want to make to the tuple generate a new tuple, as well as other characteristics of a list implementation.

In PHP it's just a constant with array value.

    
04.03.2015 / 13:11