bainternet
6/11/2013 - 12:04 PM

Simple $GLOBALS wrapper class in php

Simple $GLOBALS wrapper class in php

<?php
if ( !class_exists( 'GLB' ) ):
/**
* GLB
* 
* Simple $GLOBALS wrapper class in php
* 
* @author Ohad Raz <admin@bainternet.info>
* @copyright 2013 Ohad Raz
* @version 0.1
*/
class GLB{
  
  /**
   * set 
   * 
   * sets a var in global $GLOBALS
   * 
   * @author Ohad Raz <admin@bainternet.info>
   * @access public
   * @since 0.1
   * 
   * @param string $name  name of var
   * @param mixed $value value of var
   * 
   * @return instance
   */
  function set($name, $value){
    $GLOBALS[$name] = $value;
    return $this;
  }
  /**
   * get 
   * 
   * gets a var from global $GLOBALS
   * if name is not found the the def (default value) 
   * will be returned or false
   *
   * @author Ohad Raz <admin@bainternet.info>
   * @access public
   * @since 0.1
   * 
   * @param  string  $name name of var to get
   * @param  mixed $def  default value to return
   * 
   * @return mixed
   */
  function get($name ,$def = false){
    if(isset($GLOBALS[$name]))
      return $GLOBALS[$name];
    else
      return ($def !== false)? $def : false;
  }
  /**
   * del 
   * 
   * unsets a var in global $GLOBALS
   *
   * @author Ohad Raz <admin@bainternet.info>
   * @access public
   * @since 0.1
   * 
   * @param  string $name name of var to unset
   * 
   * @return instance
   */
  function del($name){
    unset($GLOBALS[$name]);
    return $this;
  }

  
  /**
   * fromArray 
   * 
   * sets vars in global $GLOBALS from given array
   * 
   * @author Ohad Raz <admin@bainternet.info>
   * @access public
   * @since 0.1
   * @return instance
   * 
   * @param  array $a array to set
   * 
   * @return instance
   */
  function fromArray($a=null){
    if( is_array($a) ){
      foreach($a as $k => $v) 
        $this->set($k,$v);
    }
    return $this;
  }
  /**
   * fromObject 
   * 
   * sets vars in global $GLOBALS from given objects properties
   *
   * @author Ohad Raz <admin@bainternet.info>
   * @access public
   * @since 0.1
   * @return instance
   * 
   * @param  object $a object to set
   * 
   * @return instance
   */
  function fromObject($a=null){
    if( is_object( $a ) )
      return $this->fromArray( get_object_vars($a) );
    
    return $this;
  }

  /***************
   * magic stuff *
   ***************/
  
  /**
   * __set 
   * 
   * catches set and calls set method
   *
   * @author Ohad Raz <admin@bainternet.info>
   * @access public
   * @since 0.1
   * 
   * @param string $name  
   * @param mixed $value 
   * 
   * @return instance
   */
  function __set($name,$value){
    $this->set($name,$value);
    return $this;
  }

  /**
   * __get 
   * 
   * catches get and calls get method
   *
   * @author Ohad Raz <admin@bainternet.info>
   * @access public
   * @since 0.1
   * @return instance
   * 
   * @param  string  $name
   * @param  boolean $def
   * 
   * @return mixed
   */
  function __get($name,$def = false){
    $this->get($name,$value);
  }

  /**
   * __toString 
   * 
   * returns print_r version of session array wrapped in pre tag
   *
   * @author Ohad Raz <admin@bainternet.info>
   * @access public
   * @since 0.1
   * 
   * @return string session array wrapped in pre tag
   */
  function __toString(){
    return '<pre>'.print_r($GLOBALS,true).'</pre>';
  }
}
endif;