首页 > php教程 > PHP开发 > 正文

PHP对象、模式与实践之高级特性分析

黄舟
发布: 2016-12-14 10:37:23
原创
1153 人浏览过

本文实例讲述了PHP面向对象程序设计高级特性。分享给大家供大家参考,具体如下:

高级特性

包括:

1.静态方法和属性(通过类而不是对象来访问数据和功能)
2.抽象类和接口(设计,实现分离)
3.错误处理(异常)
4.Final类和方法(限制继承)
5.拦截器(自动委托)
6.析构方法(对象销毁前的清理工作)
7.克隆对象(创建对象的副本)
8.把对象解析成字符串

PS,学会从内存的角度看代码。想象计算机的微观世界。

静态方法的小例子

<?php
class StaticExample{
  static public $aNum = 10;
  static public function sayHello(){
    print "hello";
  }
}
print StaticExample::$aNum."<br/>";
StaticExample::sayHello();
登录后复制

tips:

1.静态方法不能访问类中的普通属性,因为那些属性属于一个对象,但可以访问静态属性。
2.我们不能再对象中调用静态方法,因此不能再静态方法中使用伪变量$this。

静态方法的大例子

<?php
class ShopProduct{
  private $title;
  private $producerMainName;
  private $producerFirstName;
  protected $price;
  private $discount = 0;
  private $id = 0;
  function __construct($title,$firstName,$mainName,$price){
    $this->title = $title;
    $this->producerFirstName = $firstName;
    $this->producerMainName = $mainName;
    $this->price = $price;
  }
  public function setID($id){
    $this->id = $id;
  }
  public static function getInstance($id,PDO $pdo){
    $query = "select * from products where id= &#39;$id&#39;";
    $stmt = $pdo->query($query);
    $row = $stmt->fetch();
    if(empty($row)){
      return null;
    }
    if($row[&#39;type&#39;] == "book"){
      $product = new BookProduct($row[&#39;title&#39;],
        $row[&#39;firstname&#39;],
        $row[&#39;mainname&#39;],
        $row[&#39;price&#39;],
        $row[&#39;numpages&#39;]
        );
    }else if($row[&#39;type&#39;] == "cd"){
      $product = new CdProduct($row[&#39;title&#39;],
        $row[&#39;firstname&#39;],
        $row[&#39;mainname&#39;],
        $row[&#39;price&#39;],
        $row[&#39;playLength&#39;]
        );
    }else{
      $product = new ShopProduct($row[&#39;title&#39;],
        $row[&#39;firstname&#39;],
        $row[&#39;mainname&#39;],
        $row[&#39;price&#39;]
        );
    }
    $product->setId($row[&#39;id&#39;]);
    $product->setDiscount($row[&#39;discount&#39;]);
    return $product;
  }
  public function getProducerFirstName(){
    return $this->producerFirstName;
  }
  public function getProducerMainName(){
    return $this->producerMainName;
  }
  public function setDiscount($num){
    $this->discount = $num;
  }
  public function getDiscount(){
    return $this->discount;
  }
  public function getTitle(){
    return $this->title;
  }
  public function getPrice(){
    return ($this->price - $this->discount);
  }
  function getProducer(){
    return $this->producerFirstName." ".$this->producerMainName;
  }
  function getSummaryLine(){
    $base = "$this->title({$this->producerMainName},";
    $base .= "{$this->producerFirstName})";
    return $base;
  }
}
class CdProduct extends ShopProduct{
  private $playLength;
  function __construct($title,$firstName,$mainName,$price,$playLength){
    parent::__construct($title,$firstName,$mainName,$price);//继承父类的构造函数
    $this->playLength = $playLength;
  }
  function getPlayLength(){
    return $this->playLength;
  }
  function getSummaryLine(){
    $base = parent::getSummaryLine();
    $base .= ":playing time {$this->playLength}";
    return $base;
  }
}
class BookProduct extends ShopProduct{
  private $numPages = 0;
  function __construct($title,$firstName,$mainName,$price,$numPages){
    parent::__construct($title,$firstName,$mainName,$price);
    $this->numPages = $numPages;
  }
  function getnumPages(){
    return $this->numPages;
  }
  function getSummaryLine(){
    $base = parent::getSummaryLine();
    $base .= ":page count {$this->numPages}";
    return $base;
  }
}
$dsn = "sqlite:C:/Users/Administrator/Desktop/shop.db";
$pdo = new PDO($dsn,null,null);
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$obj = ShopProduct::getInstance(1,$pdo);
echo $obj->getSummaryLine();
登录后复制

   

希望本文所述对大家PHP程序设计有所帮助,更多相关内容请关注PHP中文网(www.php.cn)!


相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!