Object georienteerd programmeren
Code (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
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
<?php
class module
{
//This function loads a module controllerfile and headerfile. when this module contains output
//it will store the output to the corresponding output parameter and return the paramaters back to the system
public function load_module($parameters)
{
//Check if the controllerfile has been given
if(isset($parameters['controllerFile']))
{
//Load the controllerfile and return any output to the controllerOutput parameter
$controllerContent = $this->file_to_var($parameters['controllerFile']);
if(!empty($controllerContent))
{
$parameters['controllerOutput'] = $controllerContent;
}
//Check if the headerFile has been given
if(isset($parameters['headerFile']))
{
//Load the headerFile and return any output to the headerOutput parameter
$headerContent = $this->file_to_var($parameters['headerFile']);
if(!empty($headerContent))
{
$parameters['headerOutput'] = $headerContent;
}
}
}
return $parameters;
}
//The below function loads a file and returns its output
protected function file_to_var($file)
{
global $db;
if (is_file($file))
{
ob_start();
include($file);
$content = ob_get_contents();
ob_end_clean();
return $content;
}
}
}
?>
class module
{
//This function loads a module controllerfile and headerfile. when this module contains output
//it will store the output to the corresponding output parameter and return the paramaters back to the system
public function load_module($parameters)
{
//Check if the controllerfile has been given
if(isset($parameters['controllerFile']))
{
//Load the controllerfile and return any output to the controllerOutput parameter
$controllerContent = $this->file_to_var($parameters['controllerFile']);
if(!empty($controllerContent))
{
$parameters['controllerOutput'] = $controllerContent;
}
//Check if the headerFile has been given
if(isset($parameters['headerFile']))
{
//Load the headerFile and return any output to the headerOutput parameter
$headerContent = $this->file_to_var($parameters['headerFile']);
if(!empty($headerContent))
{
$parameters['headerOutput'] = $headerContent;
}
}
}
return $parameters;
}
//The below function loads a file and returns its output
protected function file_to_var($file)
{
global $db;
if (is_file($file))
{
ob_start();
include($file);
$content = ob_get_contents();
ob_end_clean();
return $content;
}
}
}
?>
Nu kan een module bijvoorbeeld op de volgende manier geladen worden:
Code (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
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
<?php
$blocks = array();
$module = new module();
if(isset($_GET['MID']) && !empty($_GET['MID']) && is_numeric($_GET['MID']))
{
$sql = 'SELECT * FROM core_modules WHERE moduleID='.$_GET['MID'].' OR blockMode=1';
}
else
{
$sql = 'SELECT * FROM core_modules WHERE home=1 OR blockMode=1';
}
if(!$ModuleQuery = $db->query($sql))
{
trigger_error('Error in the modulequery: '.$db->error);
}
else
{
while($ResultModule = $ModuleQuery->fetch_assoc())
{
$i=0;
$modules[$i]['positionTag'] = $ResultModule['positionTag'];
$modules[$i]['title'] = $ResultModule['title'];
$modules[$i]['controllerFile']= $moduleDir.$ResultModule['controllerFile'];
$modules[$i]['headerFile'] = $moduleDir.$ResultModule['headerFile'];
$modules[$i]['layoutFile'] = $templateDir.$ResultModule['layoutFile'];
//load_module uses the moduleclass and adds the controllerOutput and headerOutput
//parameters containing the output
$blocks[] = $module->load_module($modules[$i]);
$i++;
}
}
?>
$blocks = array();
$module = new module();
if(isset($_GET['MID']) && !empty($_GET['MID']) && is_numeric($_GET['MID']))
{
$sql = 'SELECT * FROM core_modules WHERE moduleID='.$_GET['MID'].' OR blockMode=1';
}
else
{
$sql = 'SELECT * FROM core_modules WHERE home=1 OR blockMode=1';
}
if(!$ModuleQuery = $db->query($sql))
{
trigger_error('Error in the modulequery: '.$db->error);
}
else
{
while($ResultModule = $ModuleQuery->fetch_assoc())
{
$i=0;
$modules[$i]['positionTag'] = $ResultModule['positionTag'];
$modules[$i]['title'] = $ResultModule['title'];
$modules[$i]['controllerFile']= $moduleDir.$ResultModule['controllerFile'];
$modules[$i]['headerFile'] = $moduleDir.$ResultModule['headerFile'];
$modules[$i]['layoutFile'] = $templateDir.$ResultModule['layoutFile'];
//load_module uses the moduleclass and adds the controllerOutput and headerOutput
//parameters containing the output
$blocks[] = $module->load_module($modules[$i]);
$i++;
}
}
?>
Wat er verder word gedaan met de output is nu natuurlijk niet van toepassing. In mijn geval heb ik hiervoor ook een class gemaakt die de template inlaad en combineerd met de output van deze modules. Die class heet uiteraard "Content". Mijn vraag is nu. IS DIT DE CORRECTE DENKWIJZE VOOR OOP????
Gewijzigd op 10/09/2010 22:57:24 door Chris visser
Bijvoorbeeld:
Een stoel is een object. Deze heeft eigenschappen (kleur, grote, aantal poten, vorm etc). Daarnaast heeft een stoel functies zoals erop zitten, verplaatsen etc.
Ook een tafel is een object. Deze heeft ook eigenschappen en methoden(functies). Echter bij een tafel horen stoelen. Dit kun je eenvoudig maken doormiddel van meerdere objecten van een stoel te maken.
Om even terug te komen op jou vraag. Hoe je nu hebt gedaan kan ik persoonlijk niet veel van OOP er nog uit halen.
Je hebt een klasse, eigenlijk maar 2 functies waarvan 1 protected is maar geen onderliggende klasse heeft (overerving). Je hebt in je 2e file eigelijk alleen PHP gebruikt en roept hier 2 functies aan.
Als ik hier bijv. title zie. Is dat een property van de klasse methode. Dus wil ik het via een setter de title zetten en dit niet gaan versturen als een verzameling (array)
Class:
Code (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
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
<?php
class module
{
private $controllerFile;
private $headerFile;
private $title;
public function __construct($controllerFile, $headerFile = '', $title)
{
if(is_file($controllerFile))
{
$this->controllerFile = $controllerFile;
}
if(is_file($headerFile))
{
$this->headerFile = $headerFile;
}
if(is_string($title))
{
$this->title = $title;
}
}
public function setOutputType($outputType)
{
if($outputType == 'body')
{
$this->file = $this->controllerFile;
}
else
{
$this->file = $this->headerFile;
}
}
public function getContent()
{
ob_start();
include($this->file);
$content = ob_get_contents();
ob_end_clean();
return $content;
}
}
?>
class module
{
private $controllerFile;
private $headerFile;
private $title;
public function __construct($controllerFile, $headerFile = '', $title)
{
if(is_file($controllerFile))
{
$this->controllerFile = $controllerFile;
}
if(is_file($headerFile))
{
$this->headerFile = $headerFile;
}
if(is_string($title))
{
$this->title = $title;
}
}
public function setOutputType($outputType)
{
if($outputType == 'body')
{
$this->file = $this->controllerFile;
}
else
{
$this->file = $this->headerFile;
}
}
public function getContent()
{
ob_start();
include($this->file);
$content = ob_get_contents();
ob_end_clean();
return $content;
}
}
?>
index.php
Code (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
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
<?php
include('classes/class_database.php');
include('classes/class_module.php');
$db = new database($host, $username, $password, $database);
if(isset($_GET['MID']) && !empty($_GET['MID']) && is_numeric($_GET['MID']))
{
$sql = 'SELECT * FROM core_modules WHERE moduleID='.$_GET['MID'].' OR blockMode=1';
}
else
{
$sql = 'SELECT * FROM core_modules WHERE home=1 OR blockMode=1';
}
if(!$ModuleQuery = $db->query($sql))
{
trigger_error('Error in the modulequery: '.$db->error);
}
else
{
$modules = array();
while($ResultModule = $ModuleQuery->fetch_assoc())
{
$modules[$ResultModule['moduleID']] = new module('Modules/'.$ResultModule['controllerFile'], 'Modules/'.$ResultModule['headerFile'], $ResultModule['title']);
}
}
$modules[1]->setOutputType('body');
echo $modules[1]->getcontent();
?>
include('classes/class_database.php');
include('classes/class_module.php');
$db = new database($host, $username, $password, $database);
if(isset($_GET['MID']) && !empty($_GET['MID']) && is_numeric($_GET['MID']))
{
$sql = 'SELECT * FROM core_modules WHERE moduleID='.$_GET['MID'].' OR blockMode=1';
}
else
{
$sql = 'SELECT * FROM core_modules WHERE home=1 OR blockMode=1';
}
if(!$ModuleQuery = $db->query($sql))
{
trigger_error('Error in the modulequery: '.$db->error);
}
else
{
$modules = array();
while($ResultModule = $ModuleQuery->fetch_assoc())
{
$modules[$ResultModule['moduleID']] = new module('Modules/'.$ResultModule['controllerFile'], 'Modules/'.$ResultModule['headerFile'], $ResultModule['title']);
}
}
$modules[1]->setOutputType('body');
echo $modules[1]->getcontent();
?>
Gewijzigd op 12/09/2010 00:21:01 door chris visser
Code (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
class module
{
private $controllerFile;
private $headerFile;
private $title;
public function __construct($controllerFile, $headerFile = '', $title)
{
if(is_file($controllerFile))
{
$this->controllerFile = $controllerFile;
}
if(is_file($headerFile))
{
$this->headerFile = $headerFile;
}
if(is_string($title))
{
$this->title = $title;
}
}
public function getTitle()
{
return $this->title;
}
public function getControllerFile()
{
return $this->controllerFile;
}
public function getHeaderFile()
{
return $this->headerFile;
}
public function getBody()
{
if(isset($this->controllerFile))
{
$output = $this->getContent($this->controllerFile);
if($output)
{
return $output;
}
}
}
public function getHeader()
{
if(isset($this->headerFile))
{
return module::getContent($this->headerFile);
}
else
{
return '';
}
}
public function getContent($file)
{
ob_start();
include($file);
$content = ob_get_contents();
ob_end_clean();
return $content;
}
}
class container extends module
{
public function setLayout($layoutFile)
{
if(is_file($layoutFile))
{
$this->layout = module::getContent($layoutFile);
}
}
public function getOutput()
{
$body = module::getBody();
$output = preg_replace('/<!-- blockcontent -->/', $body, $this->layout);
$output = preg_replace('/<!-- blocktitle -->/', $this->getTitle(), $this->layout);
return $output;
}
}
class module
{
private $controllerFile;
private $headerFile;
private $title;
public function __construct($controllerFile, $headerFile = '', $title)
{
if(is_file($controllerFile))
{
$this->controllerFile = $controllerFile;
}
if(is_file($headerFile))
{
$this->headerFile = $headerFile;
}
if(is_string($title))
{
$this->title = $title;
}
}
public function getTitle()
{
return $this->title;
}
public function getControllerFile()
{
return $this->controllerFile;
}
public function getHeaderFile()
{
return $this->headerFile;
}
public function getBody()
{
if(isset($this->controllerFile))
{
$output = $this->getContent($this->controllerFile);
if($output)
{
return $output;
}
}
}
public function getHeader()
{
if(isset($this->headerFile))
{
return module::getContent($this->headerFile);
}
else
{
return '';
}
}
public function getContent($file)
{
ob_start();
include($file);
$content = ob_get_contents();
ob_end_clean();
return $content;
}
}
class container extends module
{
public function setLayout($layoutFile)
{
if(is_file($layoutFile))
{
$this->layout = module::getContent($layoutFile);
}
}
public function getOutput()
{
$body = module::getBody();
$output = preg_replace('/<!-- blockcontent -->/', $body, $this->layout);
$output = preg_replace('/<!-- blocktitle -->/', $this->getTitle(), $this->layout);
return $output;
}
}