在本章中我们将介绍一个实例,来进一步对类加深了解。
本章构建一个page类,它代表了一个页面。其中有显示页面的title,样式,内容等函数。
此页面的效果如下:
我们将把这个页面分成几块:
1)有图的那一块。
2)有导航按钮的那一块。
3)中间文字的一会
4)页底的这一块
具体看看代码吧:
1 '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
74
75
76
77
78 service in action ltd
79
80
81
82
83 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
142 © service in action ltd.
143 请访问我们的
144 合法信息页面
145
146
147
148 $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(); 运行的效果如下:
具体代码就不说了。
代码下载点击 这里
以上就是类的应用了。