--
http://derickrethans.nl | http://ezcomponents.org | http://xdebug.org
twitter: @derickr
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
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
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
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;
}
}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
Don't you mean it's NOT valid syntax?
Derick Rethans wrote:
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;
}
}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_3const 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