-3

Before PHP 5.3 I was using the following home-made function to create enums :

function enum()
{
    for($enums = func_get_args(), $enum = reset($enums), $i = 1; $enum; $enum = next($enums), ++$i)
    {
        if(defined($enum)) throw new Exception($enum.' is already defined.');
        else define($enum, $i);
    }

} // enum()

And then :

enum('CONST0', 'CONST1', 'CONST2', ...);

With php 5.3 I can do that using const instead of define to benefit from namespaces. Is there a way I could modify this function to make it use the const keyword ?

Also I'm only using 5.3 to make my code look cleaner, if there must be a performance impact (i.e. using eval or stuffs like that), I'll stick with the good old define.

I've already looked at this question : PHP and Enumerations but I didn't find what I'm looking for.

Community
  • 1
  • 1
Virus721
  • 8,061
  • 12
  • 67
  • 123
  • If you downvote, please at least explain why. This is unconstructive and stupid. – Virus721 Nov 28 '12 at 12:03
  • 2
    Can you explain what you mean by _Is there a way i could modify this function to make it use the const keyword ?_ because this is the only question you ask. – eisberg Nov 28 '12 at 12:06
  • I don't see what isn't clear in this question. Let me ask it differently : could i use the const keyword instead of the define function inside of this function to create constants that only exist in the namespace where the enum function is called ? – Virus721 Nov 28 '12 at 12:09
  • If you want this it work in namespaces you'd have to create an enum function in all of them. – Ja͢ck Nov 28 '12 at 12:13
  • Because if a call from namespace A a function in namespace B, when the function will be executed, i will be in namespace B ? – Virus721 Nov 28 '12 at 12:15

2 Answers2

1

You can't use const keyword conditionally [see this very high voted answer].

If you want to namespace your constants, the best approach is to make them static fields of a class, taking advantage of magic __get and __set if you needed.

EDIT: no magic setters / getters in static context of course, you can come with a flexible solution anyway.

Community
  • 1
  • 1
moonwave99
  • 21,957
  • 3
  • 43
  • 64
  • Thanks for your answer. I understand now, compile time only for const. The annoying thing with using const is that you manually have to manage the value of the nums : $i = 0; const CONST0 = ++$i; const CONST1 = ++$i; etc... I wanted to avoid this too. EDIT: wait no it doesn't work either, have to hard code 1, 2, 3... – Virus721 Nov 28 '12 at 12:14
1

I will try to answer your question: Is there a way i could modify this function to make it use the const keyword ?

Short answer: no

Long answer: Yes you can, but only in an very awkward way:

<?php
function enum() {
    for($enums = func_get_args(), $enum = reset($enums), $i = 1; $enum; $enum = next($enums), ++$i) {
        eval('const ' . $enum . ' = ' . $i . ';');
    }
}

enum('CONST0', 'CONST1', 'CONST2');

Tested on http://3v4l.org/ontRN

eisberg
  • 3,731
  • 2
  • 27
  • 38
  • Yeah i've tought about this, but i think it's not worth the extra performance const. THanks for your help anyway. – Virus721 Nov 28 '12 at 12:18
  • Performance? :-O How many constants do you want to define? – eisberg Nov 28 '12 at 12:18
  • Well not so many, but i was always told eval is unoptimized and not clean. – Virus721 Nov 28 '12 at 12:20
  • Sorry to say but `enum('CONST0', 'CONST1', 'CONST2');` is not clean at all. Defining you constants one by one is. – eisberg Nov 28 '12 at 12:22
  • But why would i explicitly show their value using define() if their value does not matter ? – Virus721 Nov 28 '12 at 12:24
  • 2
    Because if you one day in the distant future notice, that `CONST1` is no longer needed and remove it all values will change and if any external data relies on this value you will be in trouble. – eisberg Nov 28 '12 at 12:25