在本章中我们将介绍一个实例,来进一步对类加深了解。
本章构建一个Page类,它代表了一个页面。其中有显示页面的title,样式,内容等函数。
此页面的效果如下:

我们将把这个页面分成几块:
1)有图的那一块。
2)有导航按钮的那一块。
3)中间文字的一会
4)页底的这一块

具体看看代码吧:


  1 <?php
  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 "<title>{$this->title}</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 "<meta name=\"keywords\" content=\"{$this->keywords}\" />";
 40      }
 41      public function DisplayPage()
 42      {
 43          print "<html>\n<header>";
 44          $this->DisplayTitle();
 45          $this->DisplayStyles();
 46          print "</header>\n<body>";
 47          $this->DisplayHeader();
 48          $this->DisplayMenus($this->buttons);
 49          $this->DisplayContent();
 50          $this->DisplayFooter();
 51          print "</body>\n</html>";
 52      }
 53      public function DisplayStyles()
 54   { 
 55  ?>   
 56   <style>
 57     h1 {color:white; font-size:24pt; text-align:center; 
 58         font-family:arial,sans-serif}
 59     .menu {color:white; font-size:12pt; text-align:center; 
 60            font-family:arial,sans-serif; font-weight:bold}
 61     td {background:black}
 62     p {color:black; font-size:12pt; text-align:justify; 
 63        font-family:arial,sans-serif}
 64     p.foot {color:white; font-size:9pt; text-align:center; 
 65             font-family:arial,sans-serif; font-weight:bold}
 66     a:link,a:visited,a:active {color:white}
 67   </style>
 68 <?php
 69   }// DisplayStyles() 
 70   
 71  public function DisplayHeader()
 72  {
 73      ?>
 74      <table width="100%" cellpadding ="12" cellspacing ="0" border ="0">
 75   <tr bgcolor ="black">
 76     <td align ="left"><img src = "logo.png" /></td>
 77     <td>
 78         <h1>Service In Action Ltd</h1>
 79     </td>
 80     <td align ="right"></td>
 81   </tr>
 82   </table>
 83      <?php
 84  }
 85  
 86  //To shoe navigation bar
 87  public function DisplayMenus($buttons)
 88  {
 89      if(is_array($buttons)&&count($buttons)==0) return;
 90      print  "<table width='100%' bgcolor='white' cellpadding='4'  cellspacing='4'\n";
 91   print "  <tr>\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 "  </tr>\n";
101   print "</table>\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 "<td width ='$width%'>
123             <a href ='$url'>
124             <img src ='s-logo.gif' alt ='$name' border ='0' /></a>
125             <a href ='$url'><span class='menu'>$name</span></a></td>";
126     }  
127     else
128     {
129       print "<td width ='$width%'>
130             <img src ='side-logo.gif'>
131             <span class='menu'>$name</span></td>";
132     }  
133   }
134   
135   //To show Footer.
136   public function DisplayFooter()
137      {
138          ?>
139          <table width = "100%" bgcolor ="black" cellpadding ="12" border ="0">
140     <tr>
141       <td>
142         <p class="foot">&copy; Service In Action Ltd.</p>
143         <p class="foot">请访问我们的
144                       <a href ="">合法信息页面</a></p>
145       </td>
146     </tr>
147     </table>
148     <?php
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 "<html>\n<header>";
         $this->DisplayTitle();
         $this->DisplayStyles();
         print "</header>\n<body>";
         $this->DisplayHeader();
         $this->DisplayMenus($this->buttons);
         $this->DisplayMenus($this->line2buttons);
         $this->DisplayContent();
         $this->DisplayFooter();
         print "</body>\n</html>";
     }
 }

$page=new ProductPage();
 $page->content="本页面用来测试我们最先进的产品!这些产品正在测试中,很快将会粉墨登场!请耐心等待";
 $page->DisplayPage();

运行的效果如下:

具体代码就不说了。
代码下载点击这里
以上就是类的应用了。

转载于:https://www.cnblogs.com/confach/articles/474687.html

PHP 14:类的实例相关推荐

  1. python教程实例-python 类和实例 - 刘江的python教程

    类和实例 阅读: 19079 评论:6 类,英文名字Class,有"类别","分类","聚类"的意思.必须牢记类是抽象的模板,用来描述具有相 ...

  2. Python类与对象最全总结大全(类、实例、属性方法、继承、派生、多态、内建函数)

    目录 面向对象编程 常用术语/名词解释 抽象/实现 封装/接口 合成 派生/继承/继承结构 泛化/特化 多态 自省/反射 访问限制 私有属性和公有属性 私有方法的设置 类 创建类 声明与定义 类属性 ...

  3. 【3万患者11万图像14类病理】NIH公开大规模胸部X光数据集

    胸部X光检查是最常见而且最具成本效益的医学影像检查之一.但是,胸部X光检查的临床诊断很具挑战性,有时被认为比胸部CT成像更难诊断.过去一些有前景的工作已经被报道过,特别是最近在结核病(TB)分类方面的 ...

  4. python命令行参数解析OptionParser类用法实例

    python命令行参数解析OptionParser类用法实例 本文实例讲述了python命令行参数解析OptionParser类的用法,分享给大家供大家参考. 具体代码如下:     from opt ...

  5. Python3中的类和实例

    1.类和实例 类是抽象的模板,实例是根据类创建出来的一个个具体的"对象".例如:动物是一大类,猫.狗.鱼等都是特定的对象,即实例.python3中使用class关键字来定义类,基本 ...

  6. 美国或将限制AI等14类新技术出口,有意针对中国

    11 月 19 日,根据 2018 年国会通过的<出口管制改革法案(Export Control Reform Act)>要求,美国商务部工业安全署(Department of Comme ...

  7. Python学习笔记__6.1章 类和实例

    # 这是学习廖雪峰老师python教程的学习笔记 1.概览 面向对象最重要的概念就是类(Class)和实例(Instance),必须牢记类是抽象的模板,比如Student类,而实例是根据类创建出来的一 ...

  8. PHP文件打包类和实例

    ASP打包很多人都是用的HSH,分享个PHP文件打包类和实例.用处还是很大的,比如我们虚拟主机打包.盗源码时打包...具体的自行研究. 测试通过: 下载地址:http://dl.dbank.com/c ...

  9. python 动态修改 类和实例 的方法

    相信很多朋友在编程的时候都会想修改一下已经写好的程序行为代码,而最常见的方式就是通过子类来重写父类的一些不满足需求的方法.比如说下面这个例子. class Dog:def bark(self):pri ...

  10. 一个类的实例是另一个类的属性python_Python中的类属性和实例属性引发的一个坑...

    问题 今天在项目中遇到了个奇怪的问题,实例的一个时间属性并没有根据时间变化而变化,分析之后恍然大悟,总结下分享给大家.大家先看一段代码: import datetime import time cla ...

最新文章

  1. 【CF EDU59 E】 Vasya and Binary String (DP)
  2. Hinton:我终于想明白大脑怎么工作了!神经学家花三十年,寻找反向传播的生物机制...
  3. VisualSVN Server以及TortoiseSVN客户端的配置和使用方法
  4. NYOJ 990 蚂蚁感冒
  5. ARM汇编加载/存储指令
  6. 为什么你还一直在穷打工?
  7. java hql 参数_java – SQLQuery中的Hibernate命名参数
  8. redis的发布和订阅模式
  9. 递归算法JAVA实现
  10. 三相三线与三相四线计算公式
  11. android layoutinflater 高度,android layoutinflater 参数
  12. 分布式架构——Gossip 协议详解
  13. 实测macOS双开微信客户端
  14. Ribbon常用配置
  15. 通过 Amazon 获取图书的封面
  16. DOCs常用命令集合cmd常用api集合
  17. CMU15-445 Lecture #04: Database Storage (Part II)
  18. Spring Cloud Gateway 不小心换了个 Web 容器就不能用了,我 TM 人傻了
  19. 梅科尔工作室-王宣-鸿蒙笔记1
  20. C语言string库(#includestring.h)

热门文章

  1. undefined reference to `major‘
  2. APK解包修改后,重新打包
  3. 粮食增产的重要性顺序,土水肥种
  4. BAT中cd某个目录时,同时转换盘符
  5. this version of the Java Runtime only recognizes class file versions up to 52.0
  6. 编程基本功:变量局部化的教训
  7. You need libtool version 1.5.14 or newer to build
  8. 编译出错 recompile with -fPIC
  9. JAVA中抓异常的办法
  10. java 数据库实现源锁_采用java和数据库两种方式进行加锁