PHP 14:類的執行個體

來源:互聯網
上載者:User
在本章中我們將介紹一個執行個體,來進一步對類加深瞭解。
本章構建一個Page類,它代表了一個頁面。其中有顯示頁面的title,樣式,內容等函數。
此頁面的效果如下:

我們將把這個頁面分成幾塊:
1)有圖的那一塊。
2)有導覽按鈕的那一塊。
3)中間文字的一會
4)頁底的這一塊

具體看看代碼吧:


1 2 class Page
3 {
4 //Page Title
5 public $title="Service In Action Ltd.";
6
7 //Page content.
8 public $content;
9
10 //Page keywords.
11 public $keywords="Service ,Action, Field Service, On Demand";
12
13 //Page tabs navigation
14 public $buttons= array('首頁'=>'Service.php',
15 '產品'=>'products.php',
16 '服務'=>'Service.php',
17 '聯絡我們'=>'Contact.php',
18 '網站地圖'=>'Sitemap.php');
19
20 //To set page's attributes, such as title,content.
21 public function __set($name,$value)
22 {
23 $this->$name=$value;
24 }
25
26 //To display title
27 public function DisplayTitle()
28 {
29 print "{$this->title}";
30 }
31 public function DisplayContent()
32 {
33 print $this->content;
34 }
35
36 //To set the header meta, and set the page keyword so that search engine to scrawl it.
37 public function DisplayKeywords()
38 {
39 print "keywords}\" />";
40 }
41 public function DisplayPage()
42 {
43 print "\n

";
44 $this->DisplayTitle();
45 $this->DisplayStyles();
46 print "\n";
47 $this->DisplayHeader();
48 $this->DisplayMenus($this->buttons);
49 $this->DisplayContent();
50 $this->DisplayFooter();
51 print "\n";
52 }
53 public function DisplayStyles()
54 {
55 ?>
56
68 69 }// DisplayStyles()
70
71 public function DisplayHeader()
72 {
73 ?>
74





75 76 77 80 81 82

78

Service In Action Ltd


79

83 84 }
85
86 //To shoe navigation bar
87 public function DisplayMenus($buttons)
88 {
89 if(is_array($buttons)&&count($buttons)==0) return;
90 print "
91 print " \n";
92
93 //calculate button size
94 $width = 100/count($buttons);
95
96 while (list($name, $url) = each($buttons))
97 {
98 $this -> DisplayButton($width, $name, $url, !$this->IsURLCurrentPage($url));
99 }
100 print " \n";
101 print "\n";
102 }
103
104 //Judge the URL is current page.
105 public function IsURLCurrentPage($url)
106 {
107 if(strpos($_SERVER['PHP_SELF'], $url )==false)
108 {
109 return false;
110 }
111 else
112 {
113 return true;
114 }
115 }
116
117 //To show the buttons.
118 public function DisplayButton($width, $name, $url, $active = true)
119 {
120 if ($active)
121 {
122 print "
123
124
125 $name";
126 }
127 else
128 {
129 print "
130
131 $name";
132 }
133 }
134
135 //To show Footer.
136 public function DisplayFooter()
137 {
138 ?>
139



140 141 146 147

142

© Service In Action Ltd.


143

請訪問我們的
144 合法資訊頁面


145

148 149 }
150 function iterateVisible() {
151 echo "MyClass::iterateVisible:\n";
152 foreach($this as $key => $value) {
153 print "$key => $value\n";
154 }
155 }
156
157 }//class Page
158
159 $page=new Page();
160 $page->content="本頁面用來測試我們最先進的服務!正在建設中,很快將會登場!";
161 $page->DisplayPage();
162 //$page->iterateVisible();
163 ?>

代碼挺長的。此代碼定義了一個Page類,此類有個方法為 DisplayPage()將整個頁面顯示出來。
此類還提供了很多方法。一一介紹一下了:
1)DisplayTitle
顯示頁面的標題。
2)DisplayContent
顯示頁面的內容,就是上面說的第3塊。
3) DisplayKeywords
顯示頁面的關鍵字
4) DisplayStyles
顯示頁面的樣式
5) DisplayHeader
顯示頁面的頭部,即上面的第一塊
6)DisplayMenus
顯示菜單或者導覽按鈕,也就是上面的第2塊
7)DisplayFooter
顯示頁尾,上面說的第4塊。
此類還是比較簡單的。不再多說。熟悉一下就可以了。
讓我們看看繼承吧。假設有一個類用來顯示Product。所以我們可以建立一個類ProductPage,並且它繼承Page。改變以下顯示的Button以及內容即可。
代碼如下:

class ProductPage extends Page
{
private $line2buttons = array ( ' PHP ' => ' PHP.php ' , ' Apache HTTP Server ' => ' Apache.php ' ,
' MySql ' => ' mysql.php ' , ' Zend Studio ' => ' ZendStudio.php ' ,
' UEStudio ' => ' http://www.ultraedit.com ' );
public function DisplayPage()
{
print " \n

" ;
$this -> DisplayTitle();
$this -> DisplayStyles();
print " \n " ;
$this -> DisplayHeader();
$this -> DisplayMenus( $this -> buttons);
$this -> DisplayMenus( $this -> line2buttons);
$this -> DisplayContent();
$this -> DisplayFooter();
print " \n " ;
}
}

$page = new ProductPage();
$page -> content = " 本頁面用來測試我們最先進的產品!這些產品正在測試中,很快將會粉墨登場!請耐心等待 " ;
$page -> DisplayPage();

啟動並執行效果如下:

具體代碼就不說了。
代碼下載點擊 這裡
以上就是類的應用了。
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.