多次包含 php 文件的 foreach 循环 - 可以接受吗?
foreach loop that include php files multiple times - acceptable?
我有这个函数 "processMessage($msg)" 可以根据其中的前几个词(前缀)处理字符串。
我从数据库中提取了很多行并通过所述函数传递每个 $msg 字符串...
这里需要注意的是,该函数没有 "if($prefix=='blah')" 条件,而是包含一堆包含这些条件的 php 文件。
为什么?
因为我不想在一个函数中硬编码一堆条件,我想通过单独的 php 文件组织每个条件以实现可移植性、自定义等(这里说来话长)
所以它基本上看起来像这样(保持代码简单):
主脚本从数据库加载行并将消息放入数组 $msg_r 然后循环遍历每个消息:
foreach (msg_r as $key=>$msg){
processMsg($msg);
}
实际处理器功能:
function processMsg($msg){
$msg_r = explode(" ",$msg); // break apart message based on spaces .. eg. "reboot machine 30"
//prepare prefixes
$prefix1 = $msg_r[0];// reboot
$prefix2 = $msg_r[1];// machine
$prefix3 = $msg_r[3];// 30
//process the above prefixes.. but instead of hard coding multiple if conditions here, load if else conditions from files.
require("condition1.php");
require("condition2.php");
require("condition3.php");
//my actual require code is in a loop that loads all files found in a target directory
}
条件文件基本上只是 php 每个目的的 if else 条件代码,例如:
if($prefix1 == 'reboot' and $prefix2 == 'machine') {
// do something
}
所以这是一个简单的设置,在我的测试中似乎有效,但我想知道这是 "normal" 还是 "acceptable" 策略,或者你们是否可以建议不同的方法?
向大家问好
使用函数数组,让每个包含文件定义一个函数并将其推送到数组中。所以主脚本将包含:
$test_array = array();
require ("condition1.php");
require ("condition2.php");
...
包含文件可以:
$test_array[] = function($prefix1, $prefix2, $prefix3) {
if ($prefix1 == 'reboot' && $prefix2 == 'machine') {
// do something
}
};
你的主要功能是:
function processMsg($msg){
global $test_array;
$msg_r = explode(" ",$msg); // break apart message based on spaces .. eg. "reboot machine 30"
//prepare prefixes
$prefix1 = $msg_r[0];// reboot
$prefix2 = $msg_r[1];// machine
$prefix3 = $msg_r[3];// 30
foreach ($test_array as $test) {
$test($prefix1, $prefix2, $prefix3);
}
}
我有这个函数 "processMessage($msg)" 可以根据其中的前几个词(前缀)处理字符串。
我从数据库中提取了很多行并通过所述函数传递每个 $msg 字符串...
这里需要注意的是,该函数没有 "if($prefix=='blah')" 条件,而是包含一堆包含这些条件的 php 文件。
为什么?
因为我不想在一个函数中硬编码一堆条件,我想通过单独的 php 文件组织每个条件以实现可移植性、自定义等(这里说来话长)
所以它基本上看起来像这样(保持代码简单):
主脚本从数据库加载行并将消息放入数组 $msg_r 然后循环遍历每个消息:
foreach (msg_r as $key=>$msg){
processMsg($msg);
}
实际处理器功能:
function processMsg($msg){
$msg_r = explode(" ",$msg); // break apart message based on spaces .. eg. "reboot machine 30"
//prepare prefixes
$prefix1 = $msg_r[0];// reboot
$prefix2 = $msg_r[1];// machine
$prefix3 = $msg_r[3];// 30
//process the above prefixes.. but instead of hard coding multiple if conditions here, load if else conditions from files.
require("condition1.php");
require("condition2.php");
require("condition3.php");
//my actual require code is in a loop that loads all files found in a target directory
}
条件文件基本上只是 php 每个目的的 if else 条件代码,例如:
if($prefix1 == 'reboot' and $prefix2 == 'machine') {
// do something
}
所以这是一个简单的设置,在我的测试中似乎有效,但我想知道这是 "normal" 还是 "acceptable" 策略,或者你们是否可以建议不同的方法?
向大家问好
使用函数数组,让每个包含文件定义一个函数并将其推送到数组中。所以主脚本将包含:
$test_array = array();
require ("condition1.php");
require ("condition2.php");
...
包含文件可以:
$test_array[] = function($prefix1, $prefix2, $prefix3) {
if ($prefix1 == 'reboot' && $prefix2 == 'machine') {
// do something
}
};
你的主要功能是:
function processMsg($msg){
global $test_array;
$msg_r = explode(" ",$msg); // break apart message based on spaces .. eg. "reboot machine 30"
//prepare prefixes
$prefix1 = $msg_r[0];// reboot
$prefix2 = $msg_r[1];// machine
$prefix3 = $msg_r[3];// 30
foreach ($test_array as $test) {
$test($prefix1, $prefix2, $prefix3);
}
}