php plugin / include issue
I'm trying to make a plugin system for one of my apps but I'm having a bit of trouble.
I'm trying to figure out how to dynamically add additional switch blocks to a switch statement using a plugin class.
Here's a basic example of what I'm trying to do, does anyone know how I could accomplish this?
switch($_GET['section']){
default:
// code here
break;
case 'test':
// code here
break;
$plugins->run('page');
}
class plugins {
function run($section){
if($section=='page'){
case 'test2':
// code here
break;
}
}
}
The case statement in the plugin开发者_运维知识库s class is what I'd like to be returned to the original switch statement. So in the plugin class I can add additional cases that can be included in the original switch statement.
So I think basically just wondering if its possible to dynamically add additional cases to the switch statement.
I think you can better achieve what you are trying to do by using classes and inheritance.
http://www.monkeycancode.com/php/php-class-tutorial
This is wrong syntax:
if($section=='page'){ // WRONG!
case 'test2':
// code here
break;
}
You can do it like this:
switch ($section) {
case 'page':
switch ($whatever) {
case 'test':
// do something
break;
}
break;
default:
// do default stuff
break;
}
It is always a good idea to use switch
instead of if else
!
精彩评论