Arata cam asa:
Cod: Selectaţi tot
class Loader {
...
public function autoloader()
{
// load DB class
$this->core('DB');
// load Lang class
$this->core('Lang', $this);
// load URI class
$this->core('URI', $this);
// load Model class
$this->core('Model', $this);
...
// DUPA ASTA FAC INSTANTIEREA CLASELOR Nume_model COPII CARE MOSTENESC CLASA PARINTE Model IN FUNCTIE DE CONTROLLERUL IN CARE MA AFLU
...
}
// core class loader
public function core($class, $param = NULL, $extraParam = NULL)
{
// if class name is empty, there's nothing to do
if (empty($class))
{
return $this;
}
// check if first parameter is an array and run this function recursively
if (is_array($class) && count($class) > 0)
{
foreach ($class as $key => $val)
{
is_int($key) ? $this->core($val, $param, $extraParam) : $this->core($key, $param, $extraParam);
}
return $this;
}
// prepare class object name
$object = strtolower($class);
// is the class already loaded?
if (isset($this->$object))
{
return $this;
}
// does the file exist?
if (!file_exists(SYSTEM_PATH . 'core/' . $object . '.php'))
{
return $this;
}
// set the class name and include file
if (class_exists($class, FALSE) === FALSE)
{
require_once SYSTEM_PATH . 'core/' . $object . '.php';
}
// instantiate class
if (!empty($extraParam))
{
$this->$object = new $class($param, $extraParam);
}
elseif (!empty($param))
{
$this->$object = new $class($param);
}
else
{
$this->$object = new $class();
}
return $this;
}
...
}
Practic pentru clasele Core cum ar fi Model, atribui o variabila locala (ex $this->$object) atunci cand le instantiez, asa incat, dupa ce am intantiat clasa DB, si am obiectul $this->db, el va fi valabil in Lang, URI, in Model, la fel cum si obiectul $this->lang va putea fi folosit in URI, si obiectul $this->uri in Model.
Clasa Model (si oricare din celelalte clase core) au constructorul asa:
Cod: Selectaţi tot
class Model
{
// -------------------------------------------
// Class constructor
// -------------------------------------------
public function __construct(&$array = array())
{
// assign all the class objects that were instantiated
// by the loader class to local class variables
if (!empty($array))
{
foreach ($array as $var => $class)
{
$this->$var = $array->$var;
}
}
var_dump($this); // imi afiseaza toate proprietatile si obiectele din Loader, ceea ce e bine
}
...
}
Dar problema e cand am o clasa gen Page_model care mosteneste clasa parinte Model.
Cod: Selectaţi tot
class Page_model extends Model
{
// -------------------------------------------
// Class constructor
// -------------------------------------------
public function __construct()
{
parent::__construct();
var_dump($this); // aici nu imi mai da toate proprietatile din Model
}
}
Am nevoie de un super-obiect cu toate proprietatile din Loader, cu toate sub-modelele, asa incat sa le pot folosi in clasele mostenite (ca-i _controller sau _model). Codeigniter rezolva problema asta f.fain, dar eu as prefera sa rezolv problema asta in Loader. Daca imi poate cineva da o sugestie...