# test - ignore

- Thread: https://externals.io/message/45637
- Original archive: https://news-web.php.net/php.internals/45637
- Started by Derick Rethans on 2009-09-27T04:32:19+00:00
- Messages: 6
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #45637 — Derick Rethans — 2009-09-27T04:32:19+00:00

URL: https://externals.io/message/45637#45637

-- 
http://derickrethans.nl | http://ezcomponents.org | http://xdebug.org
twitter: @derickr

---

## #45638 — Clint Priest — 2009-09-27T05:13:24+00:00

URL: https://externals.io/message/45637#45638 · In reply to: #45637

Are there any plans to allow bitwise declaration of flags?

Example:

class foo {
    const FLAG_1 = 1;
    const FLAG_2 = 2;
    const FLAG_3 = 4;

    private $Flags = self::FLAG_1 | self::FLAG_3
}

The above doesn't work, gets a syntax error on the | operator.

Has anyone thought about a change to allow this type of functionality?

I end up doing something like this which is less than optimal

    private $Flags = 3 /* self::FLAG_1 | self::FLAG_3 */

Thanks,

-Clint

---

## #45639 — Stan Vassilev — 2009-09-27T10:43:51+00:00

URL: https://externals.io/message/45637#45639 · In reply to: #45638

> Are there any plans to allow bitwise declaration of flags?
>
> Example:
>
> class foo {
>    const FLAG_1 = 1;
>    const FLAG_2 = 2;
>    const FLAG_3 = 4;
>
>    private $Flags = self::FLAG_1 | self::FLAG_3
> }


Hi,

This is not allowed since declaration values should be resolvable at compile 
time. I.e. they can not be an expression, but only a value literal.

While technically expression folding is possible, it's not implemented in 
any part of the PHP engine (AFAIK), and I don't think it's planned any time 
soon.

To do what you want, initialize the value in the constructor:

class foo {
    ...
    private $flags;

    function __construct()
    {
        $this->flags = self::FLAG_1 | self::FLAG_3;
    }
}

Regards,
Stan Vassilev

---

## #45640 — jvlad — 2009-09-27T14:33:28+00:00

URL: https://externals.io/message/45637#45640 · In reply to: #45639

> Hi,
>
> This is not allowed since declaration values should be resolvable at 
> compile time. I.e. they can not be an expression, but only a value 
> literal.
>
> While technically expression folding is possible, it's not implemented in 
> any part of the PHP engine (AFAIK), and I don't think it's planned any 
> time soon.
>
> To do what you want, initialize the value in the constructor:
>
> class foo {
>    ...
>    private $flags;
>
>    function __construct()
>    {
>        $this->flags = self::FLAG_1 | self::FLAG_3;
>    }
> }
>

Hi,

ok, and could you please point out to the thing in self::FLAG_1 that is 
unresolveable at compile-time?
To me it seems that nothing prevents from declaring constants based on the 
other constants. Since self is always resolveable, such declarantions can be 
allowed.

>        $this->flags = self::FLAG_1 | self::FLAG_3;

this is not the best solution because it executes the statements each time 
you create an instance of foo class, while the class constant would be 
calculated only once at compile-time.

just my 2c.

jv

---

## #45641 — Derick Rethans — 2009-09-27T14:40:42+00:00

URL: https://externals.io/message/45637#45641 · In reply to: #45640

> > […]
> 
> ok, and could you please point out to the thing in self::FLAG_1 that 
> is unresolveable at compile-time?

The following is also valid syntax:

class foo {
   private $Flags = self::FLAG_1 | self::FLAG_3

   const FLAG_1 = 1;
   const FLAG_2 = 2;
   const FLAG_3 = 4;
}

When the compiler hits self::FLAG_1 here, it's not yet defined.

Derick

-- 
http://derickrethans.nl | http://ezcomponents.org | http://xdebug.org
twitter: @derickr

---

## #45642 — Clint Priest — 2009-09-27T23:18:52+00:00

URL: https://externals.io/message/45637#45642 · In reply to: #45641

Don't you mean it's NOT valid syntax?

Derick Rethans wrote:
> 
>
>   
> > […]
>
> The following is also valid syntax:
>
> class foo {
>    private $Flags = self::FLAG_1 | self::FLAG_3
>
>    const FLAG_1 = 1;
>    const FLAG_2 = 2;
>    const FLAG_3 = 4;
> }
>
> When the compiler hits self::FLAG_1 here, it's not yet defined.
>
> Derick
