欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP對象、模式與實(shí)踐之高級(jí)特性分析

 更新時(shí)間:2016年12月08日 08:46:23   作者:牛逼的霍嘯林  
這篇文章主要介紹了PHP對象、模式與實(shí)踐之高級(jí)特性,結(jié)合實(shí)例形式分析了php面向?qū)ο蟪绦蛟O(shè)計(jì)中的靜態(tài)屬性和方法、抽象類、接口、攔截器、克隆對象等概念與簡單實(shí)現(xiàn)方法,需要的朋友可以參考下

本文實(shí)例講述了PHP面向?qū)ο蟪绦蛟O(shè)計(jì)高級(jí)特性。分享給大家供大家參考,具體如下:

高級(jí)特性

包括:

1.靜態(tài)方法和屬性(通過類而不是對象來訪問數(shù)據(jù)和功能)
2.抽象類和接口(設(shè)計(jì),實(shí)現(xiàn)分離)
3.錯(cuò)誤處理(異常)
4.Final類和方法(限制繼承)
5.攔截器(自動(dòng)委托)
6.析構(gòu)方法(對象銷毀前的清理工作)
7.克隆對象(創(chuàng)建對象的副本)
8.把對象解析成字符串

PS,學(xué)會(huì)從內(nèi)存的角度看代碼。想象計(jì)算機(jī)的微觀世界。

靜態(tài)方法的小例子

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

tips:

1.靜態(tài)方法不能訪問類中的普通屬性,因?yàn)槟切傩詫儆谝粋€(gè)對象,但可以訪問靜態(tài)屬性。
2.我們不能再對象中調(diào)用靜態(tài)方法,因此不能再靜態(tài)方法中使用偽變量$this。

靜態(tài)方法的大例子

<?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= '$id'";
    $stmt = $pdo->query($query);
    $row = $stmt->fetch();
    if(empty($row)){
      return null;
    }
    if($row['type'] == "book"){
      $product = new BookProduct($row['title'],
        $row['firstname'],
        $row['mainname'],
        $row['price'],
        $row['numpages']
        );
    }else if($row['type'] == "cd"){
      $product = new CdProduct($row['title'],
        $row['firstname'],
        $row['mainname'],
        $row['price'],
        $row['playLength']
        );
    }else{
      $product = new ShopProduct($row['title'],
        $row['firstname'],
        $row['mainname'],
        $row['price']
        );
    }
    $product->setId($row['id']);
    $product->setDiscount($row['discount']);
    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);//繼承父類的構(gòu)造函數(shù)
    $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();

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP基本語法入門教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評論