PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Pulni4kiya   Bit Options   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: An example of all the functions of the class
Class: Bit Options
Manipulate boolean values as arrays
Author: By
Last change: Updated to show the new stuff in version 3
Date: 15 years ago
Size: 2,812 bytes


Class file image Download
//Let me start with an apology for my not-quite-good English

require_once 'BitOptions.php';

//I think it's easier to use constants, than plain numbers although it's not needed.
define('USER_ACCESS_CHAT', 1);
define('USER_ACCESS_POLLS', 2);

//Normally you get an int or string from DB or file and then you create a BitOptions object with something like this:
//$user_options = (true, $read_options);
//in the above line 'true' means "use string instead of int" and $read_optinos is a string that remembers the options in the DB or file.
//Perosnally I use int for DB and string for file.

//IMPORTANT NOTE: If tou want to have more than 32 options, you must use string!!! Otherwise an exception is thrown!

//There is a constant in the class file named BIT_OPTIONS_USE_STRINGS which is used for default value of all fields
//If you want to use int always, set it to false and don't bother passing true/false to the methods if not needed

//The constant BIT_OPTIONS_BOOL_EXCEPTION determines whether an exception should be thrown or not when you pass a not-bool value where a boolean is expected
//If the constant is set to true the next line will throw an exception
BitOptions::CalculateOptions($some_options, 'not_bool');

//For this example I'll create a new object
$user_options = new BitOptions(true);

//To turn an option on use:
$user_options[USER_ACCESS_CHAT] = true;
$user_options->TurnOption(USER_ACCESS_POLLS, true);
$user_options->TurnOption(USER_ACCESS_MODERATE_CHAT, true);

//To turn it off use:
$user_options[USER_ACCESS_MODERATE_CHAT] = false;
$user_options->TurnOption(USER_ACCESS_MODERATE_CHAT, false);

//To get an array of all options that are turned on use:
$options_array = BitOptions::GetOptions($user_options);

//Lets say $read_options is a string that remembers options and we want to get the options that are turned on
$options_array = BitOptions::GetOptions($read_options, true);
//'true' again means: the options are in string format

//When you have an array ot options like:
$opt_array = array(1,6,2,8,21);
//you can create a BitOptions object by it
$all_user_options = BitOptions::CalculateOptions($opt_array, true);
//the 'true' stands again for 'use strings'

//The __toString() method is implemented so if you want to save the options in db or file you can use the variable itself (not a field)
mysql_query("... $user_options ... ");

//The fields 'options' and 'use_strings' are read only
//Once an object is created, options cannot be edited in any way but with TurnOption method

//Finally, you can loop through the options of an object that are set to true with this:
foreach ($user_options AS $v) echo "$v<br />";