Login   Register  
PHP Classes
elePHPant
Icontem

File: ExampleClass.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Vit Ek  >  Object Session Saver  >  ExampleClass.php  >  Download  
File: ExampleClass.php
Role: Example script
Content type: text/plain
Description: Example class
Class: Object Session Saver
Save and restore objects in session variables
Author: By
Last change:
Date: 7 years ago
Size: 2,359 bytes
 

Contents

Class file image Download
<?php

/**
 * including of the interface
 */
require_once(dirname(__FILE__)."/SessionObject.php");

class 
ExampleClass implements SessionObject {
    
/**
     * contains time of first initialization
     *
     * @var string
     */
    
private static $date;
    
    
/**
     * array which contains random numbers
     *
     * @var array
     */
    
private static $someArray = array();
    
    
/**
     * state of data initialization of this class
     *
     * @var boolean
     */
    
private static $dataInitialized false;
    
    function 
__construct()
    {
        if (!
self::$dataInitialized) {
            
$this->setDate();
            
$this->setUpArray();
        }
    }
    
    
/**
     * prints info about class vars
     *
     */
    
function printMyInfo()
    {
        
$this->printLine(__CLASS__);
        
$this->printLine();
        
$this->printLine("Time of my creation");
        
$this->printLine("<strong>".self::$date."</strong>");
        
$this->printLine();
        
$this->printLine("My numbers");
        print 
"<pre>";
        foreach (
self::$someArray as $key => $value) {
            
$this->printLine("<strong>{$key}</strong>:\t {$value}");
        }
        print 
"</pre>";
    }

//////////////////////////////////////    
////     PRIVATE METHODS          ////    
//////////////////////////////////////    
    
    /**
     * sets internal variable to actual date
     */
    
private function setDate()
    {
        
self::$date date("H:i:s");
    }
    
    
/**
     * sets up internal array to contain some random numbers
     *
     */
    
private function setUpArray(){
        
self::$someArray = array();
        for (
$i 0;  $i 10;  $i++ ) {
            
self::$someArray[] = rand();
        }
    }
    
    
/**
     * prints a debug line
     * @param string $str
     */
    
private function printLine($str="")
    {
        print 
"{$str}<br />";
    }
    
    
    
//////////////////////////////////////    
////      STATIC METHODS          ////    
//////////////////////////////////////    
    /**
    * returns the state of object we want to keep over each consecutive page loads
    */
    
static function getObjectState(){
        return 
serialize(array(self::$dateself::$someArray));
    }

    
/**
    * sets the object to the state previously obtained by calling object method getObjectState()
    * 
    * @param mixed $objectState 
    */
    
static function setObjectState($objectState){
        
$dataSavedToSession unserialize($objectState);
        
self::$date $dataSavedToSession[0];
        
self::$someArray $dataSavedToSession[1];
        
self::$dataInitialized true;
    }
    
}
?>