我們使用到了物件導向來宣告了一個類別DB,這個類別足夠可以處理題組一大多數的功能,但為了可以更好的利用到物件導向的特性,所以我們打算透過繼承來建立各個資料表專屬的子類別,並將各資料表專屬的功能寫在各自的類別中,這樣一來前端頁面需要的邏輯會變少,而各自類別的內容都是專屬的,藉此提高了程式碼的可讀性及簡化之後的維護及除錯工作。
同時我們也把一些好用的小函式集中放在一個檔案中,透過include的方式讓各個頁面都能使用到這些類別及函式。
建立各資料表的繼承類別
DB類別請參考先前關於DB class的解釋:
[技能檢定]網頁乙級檢定-前置作業-程式功能整合測試
在 Controller 目錄下建立以下檔案,原則上是一張資料表對應一個檔案,其中 DB.php 是父類別:

接著在每個類別檔中引入DB.php,並且建立繼承的類別:
Controller/Title.php
1 2 3 4 5 6 7 8 9
   | include_once "DB.php";
  class Title extends DB{
      function __construct()     {         parent::__construct('title');     } }
   | 
 
Controller/Ad.php
1 2 3 4 5 6 7 8 9
   | include_once "DB.php";
  class Ad extends DB{
      function __construct()     {         parent::__construct('ad');     } }
   | 
 
Controller/Mvim.php
1 2 3 4 5 6 7 8 9
   | include_once "DB.php";
  class Mvim extends DB{
      function __construct()     {         parent::__construct('mvim');     } }
   | 
 
Controller/Image.php
1 2 3 4 5 6 7 8 9
   | include_once "DB.php";
  class Image extends DB{
      function __construct()     {         parent::__construct('image');     } }
   | 
 
Controller/Total.php
1 2 3 4 5 6 7 8 9
   | include_once "DB.php";
  class Total extends DB{
      function __construct()     {         parent::__construct('total');     } }
   | 
 
Controller/Bottom.php
1 2 3 4 5 6 7 8 9
   | include_once "DB.php";
  class Bottom extends DB{
      function __construct()     {         parent::__construct('bottom');     } }
   | 
 
Controller/News.php
1 2 3 4 5 6 7 8 9
   | include_once "DB.php";
  class News extends DB{
      function __construct()     {         parent::__construct('news');     } }
   | 
 
Controller/Admin.php
1 2 3 4 5 6 7 8 9
   | include_once "DB.php";
  class Admin extends DB{
      function __construct()     {         parent::__construct('admin');     } }
   | 
 
Controller/Menu.php
1 2 3 4 5 6 7 8 9
   | include_once "DB.php";
  class Menu extends DB{
      function __construct()     {         parent::__construct('menu');     } }
   | 
 
建完各個類別後,為了後續的操作方便,我們將這些類別都引入到一個檔案中,而這個檔案又會分別被 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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
   | session_start();        date_default_timezone_set("Asia/Taipei");   
 
 
  $DIR=__DIR__; or $DIR=$_SERVER['DOCUMENT_ROOT'];
 
  include_once $DIR . "/Controller/Title.php"; include_once $DIR . "/Controller/Ad.php"; include_once $DIR . "/Controller/Mvim.php"; include_once $DIR . "/Controller/Image.php"; include_once $DIR . "/Controller/Total.php"; include_once $DIR . "/Controller/Bottom.php"; include_once $DIR . "/Controller/News.php"; include_once $DIR . "/Controller/Admin.php"; include_once $DIR . "/Controller/Menu.php";
 
 
 
 
 
  function dd($array){     echo "<pre>";     print_r($array);     echo "</pre>"; }
 
 
 
 
  function to($url){     header("location:".$url); }
 
 
 
 
  function q($sql){     $pdo=new PDO("mysql:host=localhost;charset=utf8;dbname=db13",'root','');     return $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC); }
 
  $Title=new Title; $Ad=new Ad; $Mvim=new Mvim; $Image=new Image; $Total=new Total; $Bottom=new Bottom; $News=new News; $Admin=new Admin; $Menu=new Menu;
   | 
 
接著將這個檔案引入到 index.php 及 backend.php 中:
/index.php
1 2 3 4 5
   | <?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">
   | 
 
/backend.php
1 2 3 4 5
   | <?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">
   |