Created
August 1, 2015 06:40
-
-
Save jesobreira/d68ebb4b2430bb126fb0 to your computer and use it in GitHub Desktop.
Using bitwise operators to store privs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$user_rights = 3; // 1+2 (taken from database) | |
if(rights::can_read($user_rights)) echo "User can read\n"; | |
if(rights::can_write($user_rights)) echo "User can write\n"; | |
if(rights::can_modify($user_rights)) echo "User can modify\n"; | |
class rights { | |
static $READ = 1; | |
static $WRITE = 2; | |
static $MODIFY = 4; | |
public function can_read($rights) { | |
return (($rights & self::$READ)==self::$READ); | |
} | |
public function can_write($rights) { | |
return (($rights & self::$WRITE)==self::$WRITE); | |
} | |
public function can_modify($rights) { | |
return (($rights & self::$MODIFY)==self::$MODIFY); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment