目录
Service In Action Ltd
首页 后端开发 php教程 PHP 14:类的实例

PHP 14:类的实例

Jun 23, 2016 pm 02:32 PM

在本章中我们将介绍一个实例,来进一步对类加深了解。
本章构建一个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   
PHP 14:类的实例
 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             PHP 14:类的实例

125             $name ";
126     }  
127     else
128     {
129       print "
130             PHP 14:类的实例
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(); 运行的效果如下:

具体代码就不说了。
代码下载点击 这里
以上就是类的应用了。
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

11个最佳PHP URL缩短脚本(免费和高级) 11个最佳PHP URL缩短脚本(免费和高级) Mar 03, 2025 am 10:49 AM

长URL(通常用关键字和跟踪参数都混乱)可以阻止访问者。 URL缩短脚本提供了解决方案,创建了简洁的链接,非常适合社交媒体和其他平台。 这些脚本对于单个网站很有价值

Instagram API简介 Instagram API简介 Mar 02, 2025 am 09:32 AM

在Facebook在2012年通过Facebook备受瞩目的收购之后,Instagram采用了两套API供第三方使用。这些是Instagram Graph API和Instagram Basic Display API。作为开发人员建立一个需要信息的应用程序

在Laravel中使用Flash会话数据 在Laravel中使用Flash会话数据 Mar 12, 2025 pm 05:08 PM

Laravel使用其直观的闪存方法简化了处理临时会话数据。这非常适合在您的应用程序中显示简短的消息,警报或通知。 默认情况下,数据仅针对后续请求: $请求 -

构建具有Laravel后端的React应用程序:第2部分,React 构建具有Laravel后端的React应用程序:第2部分,React Mar 04, 2025 am 09:33 AM

这是有关用Laravel后端构建React应用程序的系列的第二个也是最后一部分。在该系列的第一部分中,我们使用Laravel为基本的产品上市应用程序创建了一个RESTFUL API。在本教程中,我们将成为开发人员

简化的HTTP响应在Laravel测试中模拟了 简化的HTTP响应在Laravel测试中模拟了 Mar 12, 2025 pm 05:09 PM

Laravel 提供简洁的 HTTP 响应模拟语法,简化了 HTTP 交互测试。这种方法显着减少了代码冗余,同时使您的测试模拟更直观。 基本实现提供了多种响应类型快捷方式: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

php中的卷曲:如何在REST API中使用PHP卷曲扩展 php中的卷曲:如何在REST API中使用PHP卷曲扩展 Mar 14, 2025 am 11:42 AM

PHP客户端URL(curl)扩展是开发人员的强大工具,可以与远程服务器和REST API无缝交互。通过利用Libcurl(备受尊敬的多协议文件传输库),PHP curl促进了有效的执行

在Codecanyon上的12个最佳PHP聊天脚本 在Codecanyon上的12个最佳PHP聊天脚本 Mar 13, 2025 pm 12:08 PM

您是否想为客户最紧迫的问题提供实时的即时解决方案? 实时聊天使您可以与客户进行实时对话,并立即解决他们的问题。它允许您为您的自定义提供更快的服务

宣布 2025 年 PHP 形势调查 宣布 2025 年 PHP 形势调查 Mar 03, 2025 pm 04:20 PM

2025年的PHP景观调查调查了当前的PHP发展趋势。 它探讨了框架用法,部署方法和挑战,旨在为开发人员和企业提供见解。 该调查预计现代PHP Versio的增长

See all articles