[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/core/src/ -> lmbProxy.class.php (source)

   1  <?php
   2  /*
   3   * Limb PHP Framework
   4   *
   5   * @link http://limb-project.com 
   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
   8   */
   9  
  10  /**
  11   * Base class for creating proxies.
  12   * Proxies acts like real(original) objects until real object is definitely required.
  13   * In such a case the original object is <b>resolved</b> and since that moment all method
  14   * and argument calls will be delegeted to the real object.
  15   * @package core
  16   * @version $Id$
  17   */
  18  abstract class lmbProxy
  19  {
  20    /**

  21    * @var boolean Flag if real object is resolved already

  22    */
  23    protected $is_resolved = false;
  24    /**

  25    * @var mixed Real object

  26    */
  27    protected $original;
  28  
  29    /**

  30    * Returns hash string for original object

  31    * @return string

  32    */
  33    function getHash()
  34    {
  35      if(!$this->is_resolved)
  36        return md5(serialize($this));
  37  
  38      return $this->resolve()->getHash();
  39    }
  40  
  41    /**

  42    * Creates original object

  43    */
  44    abstract protected function _createOriginalObject();
  45  
  46    /**

  47    * Resolves original object.

  48    * Resolving is depend on child classes implementation

  49    */
  50    function resolve()
  51    {
  52      if($this->is_resolved)
  53        return $this->original;
  54  
  55      $this->original = $this->_createOriginalObject();
  56      $this->is_resolved = true;
  57  
  58      return $this->original;
  59    }
  60  
  61    /**

  62    * Magic caller

  63    * Resolves original object and delegates method call to it.

  64    */
  65    function __call($method, $args = array())
  66    {
  67      $this->resolve();
  68      if(method_exists($this->original, $method))
  69        return call_user_func_array(array($this->original, $method), $args);
  70    }
  71  
  72    /**

  73    * Magic getter

  74    * Resolves original object and delegates to it.

  75    */
  76    function __get($attr)
  77    {
  78      $this->resolve();
  79      return $this->original->$attr;
  80    }
  81  
  82    /**

  83    * Magic setter

  84    * Resolves original object and delegates to it.

  85    */
  86    function __set($attr, $val)
  87    {
  88      $this->resolve();
  89      $this->original->$attr = $val;
  90    }
  91  }
  92  ?>


Generated: Thu Oct 16 04:42:25 2008 Cross-referenced by PHPXref 0.7