Why should I use bitwise/bitmask in PHP?

后端 未结 8 1904
时光取名叫无心
时光取名叫无心 2020-11-30 20:54

I am working on a user-role / permission system in PHP for a script.

Below is a code using a bitmask method for permissions that I found on phpbuilder.com.

B

8条回答
  •  执笔经年
    2020-11-30 21:31

    The first allows people to have lots of permissions - read/add/update for example. The second example, the user has just PERMISSION_UPDATE.

    Bitwise testing works by testing bits for truth values.

    For example, the binary sequence 10010 would identify a user with PERMISSION_DELETE and PERMISSION_READ (the bit identifying PERMISSION_READ is the column for 2, the bit identifying PERMISSION_DELETE is the column for 16), 10010 in binary is 18 in decimal (16 + 2 = 18). Your second code sample doesn't allow you to do that sort of testing. You could do greater-than style checks, but that assumes everyone with PERMISSION_DELETE should also have PERMISSION_UPDATE, which may not be a valid assumption.

提交回复
热议问题