Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining bit flags in a class constant PHP [duplicate]

Possible Duplicate:
Workaround for basic syntax not being parsed

I am trying to allow developers to specify any combination of bits to specify which pieces of data they want included in a response.

    class ClassName {
            const BUILD_DATE_RFC = 1; 
            const BUILD_DATE_SQL = 2;
            const BUILD_DATE_SQLTIME = 4;
            const BUILD_DATE_UNIX = 8;

           // ....
   }

This works in the sense that when I instantiate the class like this:

$whatever = new ClassName(BUILD_DATE_RFC|BUILD_DATE_SQL);

This logic would be executed:

    if (self::BUILD_DATE_RFC & $this->metaBits) {   
        $dateMeta['RFC'] = date('r');
    }
    if (self::BUILD_DATE_SQL & $this->metaBits) {
        $dateMeta['SQL'] = date('Y-m-d');
    }
    if (self::BUILD_DATE_SQLTIME & $this->metaBits) {
        $dateMeta['SQL_time'] = date('Y-m-d H:i:s');
    }

All of this works beautifully, except I'd like to define 'shortcut bits' something like BUILD_DATE_ALL which would be the value of the sum of all the DATE related bits so they only have to specify that shortcut constant rather than each one individually.

I tried this but it throws an error:

const BUILD_DATE_ALL =  (self::BUILD_DATE_RFC|self::BUILD_DATE_SQL|self::BUILD_DATE_SQLTIME|self::BUILD_DATE_UNIX);

I also tried different approaches/syntaxes:

const BUILD_REQUEST_ALL =   self::BUILD_IP |
                self::BUILD_USERAGENT |
                self::BUILD_REFERER;

and another approach I tried:

const BUILD_DEFAULT = self::BUILD_DATE_ALL|self::BUILD_REQUEST_ALL^self::BUILD_REFERER^self::BUILD_USERAGENT;

The error I get is:

ErrorException: syntax error, unexpected '('

and the error I get for the other approaches is:

ErrorException: syntax error, unexpected '|', expecting ',' or ';'

It looks like PHP doesn't want to calculate too much in a constant definition and just wants a single value rather than derived value. I'm assuming this based off the fact that it doesn't want parenthesis nor does it want the | to do further calculations. Additionally, I tried using '-' instead of | just to test my theory.. and yes, it complained about the + being unexpected too.

How do I go about fixing the problem so I can define a 'shortcut bit' which is a sum of a range of the other already-defined constants.

like image 864
WhiskeyTangoFoxtrot Avatar asked Jan 25 '12 17:01

WhiskeyTangoFoxtrot


2 Answers

You can calculate it yourself. As these are bit flags there is a pattern.

class ClassName {
        const BUILD_DATE_RFC = 1; 
        const BUILD_DATE_SQL = 2;
        const BUILD_DATE_SQLTIME = 4;
        const BUILD_DATE_UNIX = 8;
        const BUILD_DATE_ALL = 15; // 15 = 1|2|4|8;
       // ....
}
like image 119
Shiplu Mokaddim Avatar answered Nov 01 '22 12:11

Shiplu Mokaddim


QUoting from the manual:

The value must be a constant expression, not (for example) a variable, a property, a result of a mathematical operation, or a function call.

use of the | operator is the result of an operation, therefore not pemitted

like image 1
Mark Baker Avatar answered Nov 01 '22 11:11

Mark Baker