-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.php
More file actions
45 lines (44 loc) · 1.52 KB
/
model.php
File metadata and controls
45 lines (44 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
/*
* @author Kristian Nordman <kristian.nordman@scripter.se>
*
* This is the model-model. It creates and keeps track of all instances of all models currently in use.
*/
class model{
// Instances collection
private static $instances = array();
/*
* Factory function
* Checks to see whether or not a new instance needs to be created or reuse an old one.
*
* @param string $model Name of the model to be used.
* @param string $instance Select the instance to be used. Defaults to 'default'
* @return object returns the object selected
*/
static function &factory($model, $instance = 'default'){
// Do we have an old intance available?
if(!isset(self::$instances[$model][$instance])){
// No? Well make on up then!
$model_name = 'model_'.$model;
self::$instances[$model][$instance] = new $model_name;
if(method_exists(self::$instances[$model][$instance], 'init'))
self::$instances[$model][$instance]->init();
}
// Return it
return self::$instances[$model][$instance];
}
static function __callStatic($name, $args){
if(isset($args[0])){
return self::factory($name, $args[0]);
} else {
return self::factory($name);
}
}
/*
* list instances
* Debugging-tool. Dumps all known instances
*/
static function list_instances(){
var_dump(self::$instances);
}
}