PHP Classes
Icontem

File: example.php


  Search   All class groups All class groups   Latest entries Latest entries   Top 10 charts Top 10 charts   Newsletter Newsletter   Blog Blog   Forums Forums   Help FAQ Help FAQ  
  Login   Register  
Recommend this page to a friend! ReTweet ReTweet Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Pulni4kiya  >  Bit Options  >  example.php  
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
 

Contents

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);
define('USER_ACCESS_MODERATE_CHAT'3);
define('USER_ACCESS_MANAGE_POLLS'4);

//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;
//or:
$user_options->TurnOption(USER_ACCESS_POLLStrue);
$user_options->TurnOption(USER_ACCESS_MODERATE_CHATtrue);

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

//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_optionstrue);
//'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_arraytrue);
//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 />";
?>

 
  Advertise on this site Advertise on this site   Site map Site map   Statistics Statistics   Site tips Site tips   Privacy policy Privacy policy   Contact Contact  

For more information send a message to :
info at phpclasses dot org.
Copyright (c) Icontem 1999-2009 PHP Classes - PHP Class Scripts
  PHP Book Reviews - Reviews of books and other products