Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //Bit class for settings
- abstract class BitField {
- private $value;
- public function __construct($value=0) {
- $this->value = $value;
- }
- public function getValue() {
- return $this->value;
- }
- public function get($n) {
- return ($this->value & $n) == $n;
- }
- //turns a setting Bit to ON/TRUE
- public function set($n) {
- $this->value |= $n;
- }
- //turns a setting Bit to OFF/FALSE
- public function clear($n) {
- $this->value &= ~$n;
- }
- }
- //User settings using bits and the BitField class... super cool
- class UserPrivacySettings extends BitField
- {
- const PRIVACY_EMAIL = 1;
- const PRIVACY_NAME = 2;
- const PRIVACY_ADDRESS = 4;
- const PRIVACY_PHONE = 8;
- const PRIVACY_ALL = 15;
- }
- $bf = new UserPrivacySettings();
- //Turning PRIVACY_EMAIL Setting to ON/TRUE using Bits
- $bf->set(UserPrivacySettings::PRIVACY_EMAIL);
- // Get PRIVACY_EMAIL Setting Bit value
- if ($bf->get(UserPrivacySettings::PRIVACY_EMAIL))
- {
- echo '<br><font color="green">PRIVACY_EMAIL Setting is ON/TRUE</font><br>';
- }else{
- echo '<br><font color="red">PRIVACY_EMAIL Setting is OFF/FALSE</font><br>';
- }
- // Turning PRIVACY_EMAIL Setting Bit ZOFF/FALSE
- $bf->clear(UserPrivacySettings::PRIVACY_EMAIL);
- // Get PRIVACY_EMAIL Setting Bit value
- if ($bf->get(UserPrivacySettings::PRIVACY_EMAIL))
- {
- echo '<br><font color="green">PRIVACY_EMAIL Setting is ON/TRUE</font><br>';
- }else{
- echo '<br><font color="red">PRIVACY_EMAIL Setting is OFF/FALSE</font><br>';
- }
- // The Bits value, this is the number that we will store in the users settings database or anywhere else, all the settings a user has on or off are determined from this number alone. this is the cool part and I have not seen anyone else use bit fields and stuff in PHP like my class here does, I like it.
- echo $bf->getValue();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement