<?php

namespace GriffinForms\Traits;

trait Singleton
{
	protected $data;
	
    protected function setAll() 
    {
        foreach ($this as $key => $value) {
            $key = str_replace('_', '', $key);
            $method = 'set' . ucfirst($key);
            if (method_exists($this, $method)) {
                $this->$method();
            }
        }
    }
    
    final public static function getInstance($data = null)
    {
        static $instances = array();

        $calledClass = get_called_class();

        if (!isset($instances[$calledClass]))
        {
            $instances[$calledClass] = new $calledClass($data);
        }

        return $instances[$calledClass];
    }
    
    private function __clone()
    {
        
    }
    
    protected function __construct($data)
    {
    	$this->data = $data;
        $this->setAll();
    }
}