0%

[技能檢定]題組四 步驟3 建立共用函式檔

整理好素材及開完資料表後,接著我們會製作DB類別檔,把會用到的一些功能都封裝在DB類別中,並將可能會用到一些全域變數或設定也都放在這個檔案中

建立題組專用共用函式檔

建完各個類別後,為了後續的操作方便,我們將這些類別都引入到一個檔案中,而這個檔案又會分別被 index.phpbackend.php 引入,相當於整個網站的所有頁面都可以引用到這個檔案的內容。

./api/db.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
session_start();       //啟用session功能
date_default_timezone_set("Asia/Taipei"); //設定網站的時區為亞洲台北

//簡易的自訂函式
/**
* 用來顯示陣列內容-除錯時使用
*/
function dd($array){
echo "<pre>";
print_r($array);
echo "</pre>";
}

/**
* 簡化header('location:')的使用
* 將請求導向其他檔案或頁面
*/
function to($url){
header("location:".$url);
}


//宣告各個類別的物件變數
$Bottom=new DB('bottom');
$Mem=new DB('mem');
$Admin=new DB('admins');
$Type=new DB('types');
$Goods=new DB('goods');
$Order=new DB('orders');

接著將這個檔案引入到 index.phpbackend.php 中:

/index.php/back.php

1
2
3
4
<?php include_once "./api/db.php"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- saved from url=(0039) -->
<html xmlns="http://www.w3.org/1999/xhtml">