Zend - The PHP Company




Miscellaneous

Add Code


Static Arrays  

Type: code fragment
Added by: rlaurie
Entered: 23/08/2006
Last modified: 09/12/2005
Rating: - (fewer than 3 votes)
Views: 1963
This is the method I use to work with flags and their string counter-parts when working on non-OO projects. It's nice because it keeps these flag strings out of global scope, while only using one static copy of the array through the entire lifespan of the script.


<?php
// Data Object Types

define('OBJECT_TYPE_USER',         1);
define('OBJECT_TYPE_CONTACT',     2);

function &
get_object_types()
{
    static 
$data null;
    if (
$data === null// initialization on first call
    
{
        
$data = array(
            
OBJECT_TYPE_USER => 'User',
            
OBJECT_TYPE_CONTACT => 'Contact'
            
);
    }

    return 
$data;
}
?>


Usage Example


if (isset($_POST['type']))
{
  $flags = $_POST['type'];
  $a =& get_object_types();
  echo 'This is a ', $a[$otype],' type.';



Rate This Script





Search



This Category All Categories