Hey mensen. Ik heb een class gemaakt die modules inlaad. Het is een hele simpele class en mijn doel is om het volledige OOP te laten zijn. In dit geval word de module geladen aan de hand van een moduleID. Vervolgens word de module ingeladen aan de hand van de bestanden die aan de parameters zijn gekoppeld en opgeslagen in een variabele.
<?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;
}
}
}
?>
Nu kan een module bijvoorbeeld op de volgende manier geladen worden:
<?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++;
}
}
?>
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????
2.480 views