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

Asked

Viewed 791 times

11

I found way too this new Feature of PHP 5.6.

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

Look 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 compared 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 above case.

In addition to the highlighted advantages, what are other advantages that this new implementation will bring?

This implementation of the constant accepting an array would be the same thing as the Tuple do Python?

This implementation brings some harm compared to previous versions?

  • @gmsantos, thank you so much for the correction. I just didn’t agree much with the 5.6.6 turn 5.6. 'Cause I had php 5.6.0, and this resource was NOT implemented. I just don’t know if this is due to the ALPHA phase and not the stable one

  • I’m going based on changelog. Changes like this do not appear between patch versions, they are only for bugfixes.

2 answers

7


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

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

From this version 5.6 is also already possible:

const SEC_PER_DAY = 60 * 60 * 24; 

So, yes a novelty but just that. However and as to the question...

This implementation brings some harm in relation to the versions previous?

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

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

This way it will always be compatible with previous versions of PHP.

This is a recent solution, and as we all know for certain production environments, it is not appropriate to make major changes immediately. Therefore, prudence and experience in particular should be used and in the immediate study of future implementations, but for production with some...very... care. Today and through an administrative panel it is so easy to change the version of PHP.

  • Excellent response. + 1

0

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

http://php.net/manual/en/class.splenum.php

<?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;
}

Because if you’re going to compare if a month you received is January, you use a constant and not a string prone to typo.

On the Python tuples, from what I read are similar to the Scala lists, they are immutable, any modification you want to make in the tuple generates a new tuple, and other features of implementing a list.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.