Application Variables

Derek Jones edited this page Jul 5, 2012 · 7 revisions
Clone this wiki locally

When I moved from ASP-Classic to PHP, I was pleasantly surprised by the ease of the transition. I found PHP to be a powerful language, and most of my work consisted of syntax translation, no major paradigm shifts.

However, one thing stood-out as direly missing from PHP: Due to a difference in architecture, it was not readily easy to store items in memory, so that they are available throughout the application. ASP3 had a great feature called 'application variables', these variables could be set and retrieved directly into memory, and are available globally, this was used for counters, storing application state, and basically anything that was considered global application data.

From conversations with 'native' PHP programmers, I discovered they don't often spot the benefit of a global variable store; however, programmers who came to PHP from ASP3 always say they miss it.

For those programmers, and me among them, I have written this tiny object, it does not, i regret to say, keep things in memory, but at least it is written to be file-system friendly:


<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
class Appvar
    {
    private $appVar = array();
    private $appVarPath = 'C:\applicationVariables.data';
    private $accessed = false;
    private $changed = false;

    function _firstAccess()
        {
            if(file_exists($this->appVarPath)==true)
                {
                    $file = fopen($this->appVarPath, "r");
                    $data = fread($file,filesize($this->appVarPath));
                    fclose($file);
                    $this->appVar = unserialize($data);
                }
        }

    function set($key, $val)
        {
            $this->changed = true;
            $this->appVar[$key] = $val;
        }
    
    function get($key)
        {
            if ($this->accessed == false){$this->_firstAccess();}

            $this->accessed = true;
            if(isset($this->appVar[$key])==true)
                {
                    return $this->appVar[$key];
                }
            else
                {
                    return '';
                }
        }

    function __destruct()
        {
            if ($this->changed == true)
                {
                $data = serialize($this->appVar);
                $file = fopen($this->appVarPath, "w");
            fwrite($file, $data);
            fclose($file);
          }
        }
    }
?>

Usage

  • Put this code into Appvar.php in your libraries folder.
  • If needed, change the $appVarPath variable to a path of your choice, remember to make sure PHP has write-privileges to that path. The object will write and read this file, which is basically a serialized array.
  • Load the object using 'autoload' /or/ within the controller:

$this->load->library('Appvar');
  • Setting a variable:

$this->appvar->set('<variable_name>', '<variable_value>');
  • Getting a variable:

$this->appvar->get('<variable_name>');
  • Variable value can be a number, a string or any object you may need to persist over pages.

Hope you find this object useful.

Category:Contributions::Libraries::Miscallenous