Location: PHPKode > scripts > Array to Object converter > array-to-object-converter/toobj.php
<?php
     if (!defined('PATH')) exit('Access denied.');
     /**
      * toObj
      *
      * @desc       : Converting an array to instance of toObj object
      *
      * @author     : Sebastian 'alien' Potasiak
      * @version    : 1.0.3
      * @update     : 2009-04-12
      * @copyrights : 2009 Sebastian Potasiak
      */
     class toObj
     {
          private $_variablesCount = 0; // Keeping variables count
          
          /**
           * __construct
           *
           * @desc: 'Transporting' variables
           *
           * @param: arr [array] - array to 'transport'
          */
          public function __construct($arr)
          {
               $this->_variablesCount = count($arr);
               if ($this->_variablesCount > 0)
               {
                    foreach ($arr as $key => $value)
                    {
                         $this->$key = $value;
                         if (is_array($this->$key))
                         {
                              $this->$key = new toObj($this->$key);
                         }
                    }
               }
          }

          /**
           * __set
           *
           * @desc: Adding a number to _variablesCount
           *
           * @param: {STANDARD PHP5}
          */
          public function __set($key, $value)
          {
               $this->$key = $value;
               $this->_variablesCount++;
          }

          /**
           * delete
           *
           * @desc: Deleting variable
           *
           * @param: key [string] - key of variable
           *
           * @access: public
          */
          public function delete($key)
          {
               if (isset($this->$key)) unset($this->$key);
          }
          
          /**
           * varCount
           *
           * @desc: Return count of variables
           *
           * @access: public
           * @return: int
          */
          public function varCount()
          {
               return $this->_variablesCount;
          }
     }
?>
Return current item: Array to Object converter