PHP 14:类的实例
本章构建一个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 "
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 "
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
78 Service In Action Ltd79 |
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 "
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 "
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 "