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 "
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
142 © Service In Action Ltd. 143 请访问我们的 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 "
$this -> DisplayHeader();
$this -> DisplayMenus( $this -> buttons);
$this -> DisplayMenus( $this -> line2buttons);
$this -> DisplayContent();
$this -> DisplayFooter();
print " \n " ;
}
}
$page = new ProductPage();
$page -> content = " 本页面用来测试我们最先进的产品!这些产品正在测试中,很快将会粉墨登场!请耐心等待 " ;
$page -> DisplayPage(); 运行的效果如下:
具体代码就不说了。
代码下载点击 这里
以上就是类的应用了。

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제











종종 키워드와 추적 매개 변수로 혼란스러워하는 긴 URL은 방문자를 방해 할 수 있습니다. URL 단축 스크립트는 솔루션을 제공하여 소셜 미디어 및 기타 플랫폼에 이상적인 간결한 링크를 만듭니다. 이 스크립트는 개별 웹 사이트 a에 유용합니다

Instagram은 2012 년 Facebook에서 유명한 인수에 이어 타사 사용을 위해 두 개의 API 세트를 채택했습니다. Instagram Graph API 및 Instagram Basic Display API입니다. 개발자는

Laravel은 직관적 인 플래시 방법을 사용하여 임시 세션 데이터 처리를 단순화합니다. 응용 프로그램에 간단한 메시지, 경고 또는 알림을 표시하는 데 적합합니다. 데이터는 기본적으로 후속 요청에만 지속됩니다. $ 요청-

이것은 Laravel 백엔드가있는 React Application을 구축하는 데있어 시리즈의 두 번째이자 마지막 부분입니다. 이 시리즈의 첫 번째 부분에서는 기본 제품 목록 응용 프로그램을 위해 Laravel을 사용하여 편안한 API를 만들었습니다. 이 튜토리얼에서는 Dev가 될 것입니다

Laravel은 간결한 HTTP 응답 시뮬레이션 구문을 제공하여 HTTP 상호 작용 테스트를 단순화합니다. 이 접근법은 테스트 시뮬레이션을보다 직관적으로 만들면서 코드 중복성을 크게 줄입니다. 기본 구현은 다양한 응답 유형 단축키를 제공합니다. Illuminate \ support \ Facades \ http를 사용하십시오. http :: 가짜 ([ 'google.com'=> 'Hello World', 'github.com'=> [ 'foo'=> 'bar'], 'forge.laravel.com'=>

PHP 클라이언트 URL (CURL) 확장자는 개발자를위한 강력한 도구이며 원격 서버 및 REST API와의 원활한 상호 작용을 가능하게합니다. PHP CURL은 존경받는 다중 프로모토콜 파일 전송 라이브러리 인 Libcurl을 활용하여 효율적인 execu를 용이하게합니다.

고객의 가장 긴급한 문제에 실시간 인스턴트 솔루션을 제공하고 싶습니까? 라이브 채팅을 통해 고객과 실시간 대화를 나누고 문제를 즉시 해결할 수 있습니다. 그것은 당신이 당신의 관습에 더 빠른 서비스를 제공 할 수 있도록합니다.

2025 PHP Landscape Survey는 현재 PHP 개발 동향을 조사합니다. 개발자와 비즈니스에 대한 통찰력을 제공하는 프레임 워크 사용, 배포 방법 및 과제를 탐색합니다. 이 조사는 현대 PHP Versio의 성장을 예상합니다
