整理好素材及開完資料表後,接著我們會製作DB類別檔,把會用到的一些功能都封裝在DB類別中,再透過繼承去延伸各個需要的功能
建立各資料表的繼承類別
DB類別請參考先前關於DB class的解釋:
[技能檢定]網頁乙級檢定-前置作業-程式功能整合測試
在 controller
目錄下建立以下檔案,原則上是一張資料表對應一個檔案,其中 DB.php 是父類別:

接著在每個類別檔中引入DB.php,並且建立繼承的類別:
controller/Admin.php
1 2 3 4 5 6 7 8
| include_once "DB.php";
class Admin extends DB{ function __construct() { parent::__construct('admins'); } }
|
controller/Bootom.php
1 2 3 4 5 6 7 8
| include_once "DB.php";
class Bottom extends DB{ function __construct() { parent::__construct('bottoms'); } }
|
controller/Goods.php
1 2 3 4 5 6 7 8
| include_once "DB.php";
class Goods extends DB{ function __construct() { parent::__construct('goods'); } }
|
controller/Order.php
1 2 3 4 5 6 7 8
| include_once "DB.php";
class Order extends DB{ function __construct() { parent::__construct('orders'); } }
|
controller/Type.php
1 2 3 4 5 6 7
| include_once "DB.php"; class Type extends DB{ function __construct() { parent::__construct('types'); } }
|
controller/User.php
1 2 3 4 5 6 7
| include_once "DB.php"; class User extends DB{ function __construct() { parent::__construct('users'); } }
|
建完各個類別後,為了後續的操作方便,我們將這些類別都引入到一個檔案中,而這個檔案又會分別被 index.php
及 backend.php
引入,相當於整個網站的所有頁面都可以引用到這個檔案的內容。
/base.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 30 31 32 33 34 35 36 37
| session_start(); date_default_timezone_set("Asia/Taipei");
include_once __DIR__ . "/controller/Bottom.php"; include_once __DIR__ . "/controller/User.php"; include_once __DIR__ . "/controller/Admin.php"; include_once __DIR__ . "/controller/Type.php"; include_once __DIR__ . "/controller/Goods.php"; include_once __DIR__ . "/controller/Order.php";
function dd($array){ echo "<pre>"; print_r($array); echo "</pre>"; }
function to($url){ header("location:".$url); }
$Bottom=new Bottom; $User=new User; $Admin=new Admin; $Type=new Type; $Goods=new Goods; $Order=new Order;
|
接著將這個檔案引入到 index.php
及 backend.php
中:
/index.php 、 /backend.php
1 2 3 4
| <?php include_once "base.php"; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
|