CodeIgniter Forums

Full Version: Data Storage Object PHP5
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]wiredesignz[/eluser]
Code:
<?php
/**
* Data Storage Object PHP5
*
* This library can be used to store and retrieve sets of data.
*
* Use in views or anywhere where you may have an incomplete dataset
* it will prevent undefined variable errors. ie: Use in forms during insert.
*  
* Example:
* $user = new DataStore($this->user_model->get('Admin'));
* $user->languages = $this->language_model->get($user->id);
*
* @version: 0.2 (c) Wiredesignz 2008-09-10
*/
class DataStore
{
    private $datastore;
    
    public function __construct($data = array())
    {
        $this->datastore = $data;
    }

    public function __set($key, $data = NULL)
    {
        if (is_array($this->datastore))  
            
            $this->datastore[$key] = $data;
        
        if (is_object($this->datastore))
        
            $this->datastore->$key = $data;
    }
    
    public function __get($key)
    {
        if (is_array($this->datastore) AND isset($this->datastore[$key]))
        
            return $this->datastore[$key];
        
        if (is_object($this->datastore) AND isset($this->datastore->$key))
        
            return $this->datastore->$key;
            
        return NULL;
    }
}

El Forum

[eluser]Mark van der Walle[/eluser]
Hmmm looks nice and simple. Perhaps I will use it for my object/fragment cache implementation. (It uses a member variable as a sort of internal cache for items that I already retrieved or that have been set in the current request)

El Forum

[eluser]Colin Williams[/eluser]
wiredesignz has been lovin' getters and setters lately. Need to start considering them more myself, and overall, get away from a PHP 4 mindset.