Return to Snippet

Revision: 359
at July 12, 2006 05:36 by ekobudisetiyo


Updated Code
//PHP5 only
class fuselogic_container
{
   var $__x = array();

   function __construct($path = null)
   {
      static $instances = array();
      $class_name = strtolower(get_class($this));
      if(!isset($instances[$class_name]))
      {
         $instances[$class_name] = $this;
      }
      foreach(get_class_vars($class_name) as $var => $value)
      {
         $this->$var = &$instances[$class_name]->$var;
      }
      $this->__path = isset($path)?$path:getcwd();
   }

   function __get($name)
   {
       $lowercase = strtolower($name);
       if(isset($this->__x[$lowercase]))
       {
          return $this->__x[$lowercase];
       }else
       {
          $this->__x[$lowercase] = $this->$name();
          $instance = &$this->__x[$lowercase];
          return $instance;
       }
   }

   function __call($name,$var)
   {
       if(class_exists($name))
       {
          if(isset($var[0]))
          {
             return new $name($var[0]);
          }else
          {
             return new $name();
          }
       }else
       {
          $this->_start();
          if(@include_once('class.'.$name.'.php'))
          {
             return new $name();
          }
          $this->_end();
       }
   }

   function _start()
   {
      $this->__path_back = getcwd();
      chdir($this->__path);
   }

   function _end()
   {
      chdir($this->__path_back);
   }

   function __set($name,$val)
   {
       $this->__x[$name] = $val;
   }

   function __isset($name)
   {
      $name = strtolower($name);
      if(isset($this->__x[$name]))
      {
         return true;
      }elseif(method_exists($this,$name))
      {
         return true;
      }else return false;
   }
}

Revision: 358
at July 11, 2006 00:31 by ekobudisetiyo


Initial Code
<?php
//PHP5 only
class fuselogic_container
{
   var $__x = array();

   function __construct($path = null)
   {
      static $instances = array();
      $class_name = strtolower(get_class($this));
      if(!isset($instances[$class_name]))
      {
         $instances[$class_name] = $this;
      }
      foreach(get_class_vars($class_name) as $var => $value)
      {
         $this->$var = &$instances[$class_name]->$var;
      }
      $this->__path = isset($path)?$path:getcwd();
   }

   function __get($name)
   {
       $lowercase = strtolower($name);
       if(isset($this->__x[$lowercase]))
       {
          return $this->__x[$lowercase];
       }else
       {
          $this->__x[$lowercase] = $this->$name();
          $instance = &$this->__x[$lowercase];
          return $instance;
       }
   }

   function __call($name,$var)
   {
       if(class_exists($name))
       {
          if(isset($var[0]))
          {
             return new $name($var[0]);
          }else
          {
             return new $name();
          }
       }else
       {
          $this->_start();
          if(@include_once('class.'.$name.'.php'))
          {
             return new $name();
          }
          $this->_end();
       }
   }

   function _start()
   {
      $this->__path_back = getcwd();
      chdir($this->__path);
   }

   function _end()
   {
      chdir($this->__path_back);
   }

   function __set($name,$val)
   {
       $this->__x[$name] = $val;
   }

   function __isset($name)
   {
      $name = strtolower($name);
      if(isset($this->__x[$name]))
      {
         return true;
      }elseif(method_exists($this,$name))
      {
         return true;
      }else return false;
   }
}
?>

Initial URL


Initial Description
Very usefull lite dependency injection for PHP5.
Usage:

di exends fuselogic_container()
{
   function __construct()
   {
      parant::__construct(__FILE__);
   }
}

safe this to the folder with others class with name "class.class_name.php'
then you can do like bellow

$di = new di();
$c1 = $di->class_x; //singleton
$c2 = $di->class_x; //singleton
$c3 = $di->class_x(); //not singleton

Initial Title
FuseLogic - Lite version of Dependency Injection

Initial Tags


Initial Language
PHP