Skip to content

Commit 60d3174

Browse files
committed
Autoload() functionality
1 parent f358f5e commit 60d3174

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

dbObject.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ class dbObject {
3939
* @var MysqliDb
4040
*/
4141
private $db;
42+
/**
43+
* Models path
44+
*
45+
* @var modelPath
46+
*/
47+
private static $modelPath;
4248
/**
4349
* An array that holds object data
4450
*
@@ -548,5 +554,25 @@ private function prepareData () {
548554
}
549555
return $sqlData;
550556
}
557+
558+
private static function dbObjectAutoload ($classname) {
559+
$filename = "models/". $classname .".php";
560+
include ($filename);
561+
}
562+
563+
/*
564+
* Enable models autoload from a specified path
565+
*
566+
* Calling autoload() without path will set path to dbObjectPath/models/ directory
567+
*
568+
* @param string $path
569+
*/
570+
public static function autoload ($path = null) {
571+
if ($path)
572+
static::$modelPath = $path . "/";
573+
else
574+
static::$modelPath = __DIR__ . "/models/";
575+
spl_autoload_register ("dbObject::dbObjectAutoload");
576+
}
551577
}
552578
?>

tests/dbObjectTests.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@
33
require_once ("../MysqliDb.php");
44
require_once ("../dbObject.php");
55

6-
function __autoload ($classname) {
7-
$filename = "models/". $classname .".php";
8-
include_once ($filename);
9-
}
10-
116
$db = new Mysqlidb('localhost', 'root', '', 'testdb');
7+
dbObject::autoload ("models");
8+
129
$tables = Array (
1310
'users' => Array (
1411
'login' => 'char(10) not null',

0 commit comments

Comments
 (0)