Reputation: 458
I have a menu which creates a GET statement in the url
<li><a href="?Page=Contact">Contact</a>
. This get is used to get the corresponding content.
As in, the url will look like ?Page=Contact
and than it will load the content from Contact.
Now in another file i have a switch that checks the GET statement in the url.
$GetStatement = $ConfigPage->getFormVariable('Page');
switch ($GetStatement){
case "Home":
$Content = new ContentHome();
$ConfigPage->SetProperty('content', $Content);
break;
case "Contact":
$Content = new ContentContact();
$ConfigPage->SetProperty('content', $Content);
break;
}
Of course there are more cases in this switch, but it's useless to show. Now this switch works flawless. But as my content grows i have to keep adding more cases. And now i am at the point i want this to be automated. Of course i have tried to. but now i have literally no idea how to do this, or what to do.
Edit:
All the different content are in different files. With all unique class name. as you can see above. ContentContact is inside file Contact.php with a class named ContentContact
Upvotes: 0
Views: 79
Reputation: 775
Something like this could do the trick:
function getContentInstance($stmt) {
$name = 'Content'.$stmt;
$path = 'Content/'.str_replace(".", "", $name).'.class.php'; // needs to do even more ...
if(class_exists($name) {
return new $name();
} else {
if (file_exists($path)) {
include $path;
return new $name();
}
user_error('Class '.$name.' not found');
}
}
Upvotes: 0
Reputation: 31634
While that's not a very efficient setup (I don't know that I would create one class per page) what you could do is create a function that would do the work of looking for your class for you
function loadClass($name) {
$class_name = 'Content' . $name;
if(!class_exists($class_name)) return false;
$class = new $class_name();
return $class;
}
$class = loadClass($ConfigPage->getFormVariable('Page'));
if($class) $ConfigPage->SetProperty('content', $class);
Upvotes: 3