首頁 >後端開發 >php教程 >PHP物件、模式與實務之高階特性案例分析

PHP物件、模式與實務之高階特性案例分析

墨辰丷
墨辰丷原創
2018-05-29 16:48:191349瀏覽

這篇文章主要介紹了PHP物件、模式與實務之高階特性,結合實例形式分析了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物件導向

php物件導向之克隆物件

php中物件的序列化,php物件串列化

######################### ###########

以上是PHP物件、模式與實務之高階特性案例分析的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn