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????
Denkwijze is heel eenvoudig...denk in objecten.
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)
Okok, ik heb de module herschreven. Ik probeer het te leren en dit zijn slechts voorbeelden natuurlijk. Hieronder eerst mijn 2e versie van de module class en daaronder het procedurele gedeelte.

Class:
<?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;
}
}

?>
index.php
<?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();
?>
Ik heb inmiddels mijn module class zoveel mogelijk voltooid mbv de OOP tutorials die te vinden zijn op deze website. Ik ben erg benieuwd. Zelf merk ik nu echt de voordelen die het even later gaat geven. De module class laad een module in op basis van de gegevens (links naar bestanden) die het systeem geeft. Het is in staat om bestanden in te laden en de content terug te geven en controleert of de links daadwerkelijk bestanden zijn. Vervolgens heb ik een extend gemaakt die de layout van de module bijvoegt. Dit is voor alsnog een experiment, maar het werkt al aardig. Op en aanmerkingen zijn welkom!

<?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;
}
}

Reageren