PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Nathaniel H   Bitwise Flags   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Example file
Class: Bitwise Flags
Manipulate sets of bit values
Author: By
Last change: - Fixed a documentation error (missing value in setFlag function call)
- Fixed a formatting typo
Date: 13 years ago
Size: 5,264 bytes
 

Contents

Class file image Download
<?php
include('Flags.class.php');
session_start();
$statusList = array();

/**
 * In this example, the page allows the viewer to specify the number of flags, meaning
 * we need to retrieve the number of flags from the session as well as check for a change
 * in that number (through the form provided).
 * We also store the number of flags in the session for further page views.
**/
if(is_array($_SESSION) && array_key_exists('flagnum',$_SESSION)) {
   
$numberOfFlags = $_SESSION['flagnum'];
} else {
   
$numberOfFlags = 62;
}
if(
is_array($_POST) && array_key_exists('flagnumsub',$_POST)) {
   
$numberOfFlags = (int)$_POST['numberofflags'];
}
$_SESSION['flagnum'] = $numberOfFlags;


/**
 * Generate the flag name list.
 * For the purposes of this example, flags are generated dynamically using
 * a naming template (fXX where XX is the flag number).
**/
for($i=count($statusList);$i<$numberOfFlags;$i++) {
   
$statusList[$i] = 'flag'.($i+1);
}

/**
 * Create our flags class...
**/
$state = new Flags($statusList);

/**
 * First off, because the example requires multiple page loads, we check to see if
 * the session is currently holding any flag data.
 *
 * If it is, we load that data into the flags class.
 *
 * If not, we set the initial values to true (which showcases the maximum footprint
 * of the class).
**/
if(is_array($_SESSION) && array_key_exists('flags',$_SESSION)) {
   
// Load flags from session
   
$state->setData($_SESSION['flags']);
} else {
   
// Set all flags to true.
   
for($i=0,$l=count($statusList);$i<$l;$i++) {
       
$state->{$statusList[$i]} = true;
    }
}

/**
 * Because we allow the viewer to toggle the flag state, we now loop through and check
 * to see if it was toggled. If it is, toggle it. Otherwise, nothing happens.
 *
 * ------------------------------------------------------------------------------------
 *
 * Be aware that this example only toggles flags. You can also set flags to an explicit
 * boolean value like so:
 *
 * $flagClassInstance->flagName = true; // sets flagName to true ('on')
 * $flagClassInstance->flagName = false; // sets flagName to false ('off')
 *
 * $flagClassInstance->setFlag('flagName',true); // sets flagName to true ('on')
 * $flagClassInstance->setFlag('flagName',false); // sets flagName to false ('off')
**/
foreach($statusList as $i=>$status) {
    if(
is_array($_POST) && array_key_exists('sub'.$status,$_POST)) {
       
/**
         * All that is required to toggle the flag is to call:
         * $flagClassInstance->flagName();
        **/
       
$state->$status();
    }
}

/**
 * Again, because the example spans multiple page views, store the updated flag data in
 * the session.
**/
$_SESSION['flags'] = $state->getData();


/**
 * And now, we start building the interface.
 * Start with echoing the current state (built-in __toString method to automatically
 * format the class conversion to string for display. Will show the state of each flag
 * as '1' or '0', in order of it's bit).
**/
echo 'Flag value:<br />'.$state.'<br /><br /><br />';

/**
 * Quick form to allow the user to set the number of flags
**/
echo "
<form name='flagnum' action='index.php' method='post'>
Number of flags: <input type='text' name='numberofflags' id='numberofflags' value='
{$numberOfFlags}' /><br />
<input type='submit' name='flagnumsub' id='flagnumsub' value='Change' />
</form>
"
;

/**
 * Some quick formatting variables.
 * $perLine = number of flags to show per table row
 * $currentPer = used within the loop.
 * Neither are of significant value.
**/
$perLine = 20;
$currentPer = 0;
echo
'<table><tr>';
for(
$i=count($statusList)-1,$l=0;$i>=$l;$i--) {
   
$status = $statusList[$i];
   
   
// Form to show the toggle button...
   
$stateForm = "
<form name='changeState
{$status}' action='index.php' method='post' style='display: inline;'>
<input type='submit' name='sub
{$status}' id='sub{$status}' value='toggle' />
</form>
    "
;
   
    echo
'<td style="text-align: center;">';
    echo
$status.'<br />';
    echo (
$state->$status ? 'Yes' : 'No' ).'<br />'; // $flagClassInstance->FlagName will return the flag value.
   
echo ( $state->isFlagSet($status) ? '1' : '0' ).'<br />'; // $flagClassInstance->isFlagSet('flagName') will also return the flag value.
   
echo $stateForm; // The form generated above...
   
echo '</td>';
   
   
// Below is formatting code...
   
$currentPer++;
    if(
$currentPer%$perLine == 0) {
        echo
'</tr>'
           
.'<tr><td colspan="'.$perLine.'"><hr /></td>'
           
.'<tr>';
    }
}
echo
'</tr></table>';

/**
 * Here, we show the viewer how the data is stored. It is in a seriealized array of integers.
 *
 * We also show the overall length of the flag data.
 *
 * Though not shown, the length of the data string is on average less than the length required
 * to store an array of boolean values. Ultimately, this is a great tool for large amounts of
 * boolean values, though the advantages may be lost below a certain amount of values.
**/
echo 'To Store:<br />';
$toStore = $state->getData();
echo
$toStore.'<br />';
echo
'Length: '.strlen($toStore);
?>