简体   繁体   English

多个文件上的切换语句(例如,用于切换情况语句的插件系统)

[英]Switch-Statement over multiple files (e.g. Plugin-System for switch-case-statements)

I want to create something like a plugin-system for dynamic switch statements like in the following example: 我想为动态切换语句创建类似于插件系统的示例,如以下示例所示:

$command = $_POST['cmd'];
switch ($command) {

 include "./Ajax_Includer.php";

}

The File "Ajax_Includer" should now load all files in a specific folder. 现在,文件“ Ajax_Includer”应将所有文件加载到特定文件夹中。 These files all contain one case-statement. 这些文件都包含一个案例声明。

The advantage of this should be that I can add as many case-statements as my fingers can. 这样做的好处是,我可以添加尽可能多的案例陈述。 And also the removal of the case statements would be easier by just killing or ignoring the file with the specific case-statement. 而且,仅通过杀死或忽略具有特定case语句的文件,即可更轻松地删除case语句。

But I get an Error that I cannot use "Include" directly inside a switch. 但是我收到一个错误,我不能在开关内直接使用“包含”。 Does anyone know a workaround or is this not possible? 有谁知道解决方法,或者这不可能吗?

Break it like: 像这样打破它:

switch ($command) {
    case 'your_first_case':
        $file = "./Ajax_Includer.php";
        break;

    default:
        $file = null;
        break;
}
if($file){
    include $file
}

Although including files in switch is perfectly valid. 尽管在switch包含文件是完全有效的。


Update: If you want your 'case's in the included file, you'll get yourself a parse error, as switch() is 更新:如果要在包含的文件中添加“ case”,则将获得一个解析错误,因为switch()

expecting case (T_CASE) or default (T_DEFAULT) or '}' 预期大小写(T_CASE)或默认(T_DEFAULT)或'}'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM