By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,722 Members | 1,227 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,722 IT Pros & Developers. It's quick & easy.

How to initialize a class property with binary value? (bitmasks)

P: n/a
Lets run following code:

------- snip ------

class Perm {
static $read = bindec('001');
static $write = bindec('010');
static $delete = bindec('100');
}

print Perm::$read & Perm::$write;

------- snap ------

Output:

Parse error: parse error, unexpected '(', expecting ',' or ';' in ...
So, how do I get around that, i.e. initialize a class property with binary
value? Separate method or calculator are not very beautiful solutions.
--
Jaakko Holster

Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
.oO(Jaakko Holster)
class Perm {
static $read = bindec('001');
static $write = bindec('010');
static $delete = bindec('100');
}

print Perm::$read & Perm::$write;

------- snap ------

Output:

Parse error: parse error, unexpected '(', expecting ',' or ';' in ...
So, how do I get around that, i.e. initialize a class property with binary
value? Separate method or calculator are not very beautiful solutions.


That's what a constructor is for, but in this case ... why not simply
put in the values directly (1, 2, 4)? Or use predefined constants:

define('P_READ', bindec('001'));
....

Micha
Jul 17 '05 #2

P: n/a
>>So, how do I get around that, i.e. initialize a class property with binary
value? Separate method or calculator are not very beautiful solutions.
That's what a constructor is for, but in this case ... why not simply
put in the values directly (1, 2, 4)?


It's easier to write bitmasks in binary format than in decimal format...
define('P_READ', bindec('001'));


....whereas global constants are not very beautiful from point of view of
oop.

What I'm saying, PHP should consider 0010110b strings as binary value
(similar to 0xff -> hex syntax).

Well, if you cant have the best - I'm satisfied with following code:

class Perm {
const read = 1; // 001b
const write = 2; // 010b
const list = 4; // 100b
...etc...
}

print Perm::read;

--
Jaakko Holster

Jul 17 '05 #3

P: n/a
.oO(Jaakko Holster)
It's easier to write bitmasks in binary format than in decimal format...


Fo such things I used hexadecimal format a while ago.

Micha
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.