How to make an array with arguments that have two variables each?

Asked

Viewed 23 times

-1

The lines below repeat several times:

define("NS_CIRCULAR", 3068);
define("NS_CIRCULAR_TALK", 3069);
$wgExtraNamespaces[NS_CIRCULAR] = "Circular";
$wgExtraNamespaces[NS_CIRCULAR_TALK] = "Circular_talk";
$smwgNamespacesWithSemanticLinks[NS_CIRCULAR] = true;

define("NS_DESPACHO", 3070);
define("NS_DESPACHO_TALK", 3071);
$wgExtraNamespaces[NS_DESPACHO] = "Despacho";
$wgExtraNamespaces[NS_DESPACHO_TALK] = "Despacho_talk";
$smwgNamespacesWithSemanticLinks[NS_DESPACHO] = true;

To save lines, you can make an array for "define", one for "$wgExtraNamespaces" and one for "$smwgNamespacesWithSemanticLinks" without breaking php?

1 answer

1

I don’t know if an array defines, even if possible, is the best solution for this.

Probably the use of ENUM, but I don’t remember if PHP would. If not, a close solution could be to create a class with Constant inside.

Example:

<?php
class enum
    {
    const X = 0;
    const Y = 1;
    const Z = 2;
    }

printf("Escreva Enum X:: %d\n", enum::X);
printf("Escreva Enum Y:: %d\n", enum::Y);
printf("Escreva Enum Z:: %d\n", enum::Z);
?>

Of course, there are differences between define and const

Another solution, which I think would suit you, would be the use of associative array.

<php
$enum = array
    (
    'X'=>0,
    'Y'=>1,
    'Z'=>2
    );

printf("Escreva Enum X:: %d\n", $enum['X']);
printf("Escreva Enum Y:: %d\n", $enum['Y']);
printf("Escreva Enum Z:: %d\n", $enum['Z']);
?>

Even so, if you want to keep them, it would be better if they stayed at the top of the file or even in a separate file only with Defines. Aligned one beneath the other. Easier to find later and more organized.

  • Ah, even with the class name Enum one could put class NS { its constants }, would call them statically, would only be more organized by the fact of being a class, but I think in the context of use would be similar to the define.

Browser other questions tagged

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