如何将PHP函数转换为非函数?
如何将下面代码中的php函数转换为非函数如何将PHP函数转换为非函数?,php,refactoring,Php,Refactoring,如何将下面代码中的php函数转换为非函数 <?php require_once ('./mysqli_connect.php'); // Connect to the db. function make_list ($parent) { global $tasks; echo '<ol>'; foreach ($parent as $task_id => $todo) { echo "<li>$todo";
<?php
require_once ('./mysqli_connect.php'); // Connect to the db.
function make_list ($parent)
{
global $tasks;
echo '<ol>';
foreach ($parent as $task_id => $todo)
{
echo "<li>$todo";
if (isset($tasks[$task_id]))
{
make_list($tasks[$task_id]);
}
echo '</li>';
}
// Close the ordered list:
echo '</ol>';
}
$mysqli = new mysqli("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT task_id, parent_id, task FROM tasks WHERE date_completed='0000-00-00 00:00:00' ORDER BY parent_id, date_added ASC");
if (!$dbc)
{
// There was an error...do something about it here...
print mysqli_error();
}
$tasks = array();
while (list($task_id, $parent_id, $task) = mysqli_fetch_array($dbc, MYSQLI_NUM))
{
// Add to the array:
$tasks[$parent_id][$task_id] = $task;
}
make_list($tasks[0]);
mysqli_close(); // close the connection
// Include the html footer
include('./includes/footer.html');
?>
即使我没有发布的代码的其余部分是非函数形式的,还是让我的代码保持这样比较好。首先:不能简单地将递归函数转换为意大利面代码。
第二:这样做没有意义。将逻辑、操作以及数据的逻辑和表示分离为函数。不要用html标记垃圾邮件发送代码。使用某种模板机制。除了我完全支持的erenon的答案之外,他已经指出了你的问题走向错误方向的原因: 使用或创建一些隐藏连接、查询执行等的数据库抽象类,使以后更改数据库或适应接口更改变得更加容易,并且您有一个错误处理的中心位置
不要以超级用户的身份与数据库交谈 我认为更好的问题是:为什么你想把你的函数变成非函数?