6

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.

Community
  • 1
  • 1
  • You need to define that constant with a plain numeric value (from adding the flags yourself). – mario Jan 25 '12 at 17:20
  • From the [docs](http://php.net/manual/en/language.oop5.constants.php), constant expression support was added in PHP 5.6.0. So your following initial approach would not throw an error. `const BUILD_DATE_ALL = (self::BUILD_DATE_RFC|self::BUILD_DATE_SQL|self::BUILD_DATE_SQLTIME|self::BUILD_DATE_UNIX);` – Katrina Nov 08 '17 at 18:16

2 Answers2

6

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;
       // ....
}
Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
  • 1
    Kind of annoying because I wanted to show the bits actually being included so another dev can look at it and figure it out, but that's what comments are for. This workaround is what I'll implement. Thanks! – WhiskeyTangoFoxtrot Jan 25 '12 at 17:32
  • 11
    If you set `BUILD_DATE_ALL = -1` then you wouldn't need to work it out, because `-1` sets all the bits to 1 anyway. Best practice for an ALL flag. – worldofjr Oct 08 '14 at 13:38
1

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

Mark Baker
  • 209,507
  • 32
  • 346
  • 385