Ik en Saven gaan een modulair systeem maken
Eerste stap(zit nog een error in, hij zegt dat de class niet bestaat), zal wel niet al te best zijn, maar Saven gaat daar hopelijk verbetering in brengen!
include/core.inc.php
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
46
47
48
<?php
/**
* @author Sander
* @copyright 2007
*/
class core {
private $__loaded_modules = array( );
public function load_modules( $array ) {
if( is_array( $array ) ) {
foreach( $array as $key => $value ) {
require_once( $array[ $key ][ 'path' ] );
if( class_exists( $this->modules->$array[ $key ][ 'classname' ] ) ) {
$this->modules->{ $array[ $key ][ 'classname' ] } = new $array[ $key ][ 'classname' ];
$this->modules->{ $array[ $key ][ 'classname' ] }->core = &$this;
if( $array[ $key ][ 'start' ] == TRUE ){
$this->modules->{ $array[ $key ][ 'classname' ] }->start();
}
} else {
$this->error( 'core::load_modules: class( ' . $array[ $key ][ 'classname' ] . ') does not exists');
}
}
} else {
$this->error( 'core::load_modules: can only load arrays');
}
}
public function error( $the_error ) {
die( 'Error: ' . $the_error );
}
}
/* REMOVE LATER */
$core = new core();
$modules = array(
'mysql' => array(
'path' => 'classes/mysql.php',
'classname' => 'MySQL',
'start' => TRUE
)
);
$core->load_modules( $modules );
/* /REMOVE LATER */
?>
classes/mysql.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
/**
* @author Sander
* @copyright 2007
*/
class MySQL {
private $core;
public function start() {
echo 'MySQL started';
echo 'Core: loaded modules:' . $this->core->__loaded_modules;
}
}
?>
index.php
1
2
3
4
5
6
7
8
9
10
11
<?php
/**
* @author Sander
* @copyright 2007
*/
include( 'include/core.inc.php' );
echo '1337';
?>