Home >php教程 >PHP开发 >Advanced feature analysis of PHP objects, patterns and practices

Advanced feature analysis of PHP objects, patterns and practices

黄舟
黄舟Original
2016-12-14 10:37:231164browse

The examples in this article describe the advanced features of PHP object-oriented programming. Share it with everyone for your reference, as follows:

Advanced features

include:

1. Static methods and properties (access data and functions through classes instead of objects)
2. Abstract classes and interfaces (separation of design and implementation )
3. Error handling (exceptions)
4. Final classes and methods (restricted inheritance)
5. Interceptors (automatic delegation)
6. Destructor methods (cleanup before object destruction)
7. Clone objects (create A copy of the object)
8. Parse the object into a string

PS, learn to look at the code from the perspective of memory. Imagine the microscopic world of computers.

Small example of static method

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

tips:

1. Static methods cannot access ordinary properties in a class because those properties belong to an object, but static properties can be accessed.
2. We cannot call static methods in objects, so we cannot use the pseudo variable $this in static methods.

Big example of static method

<?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();

I hope this article will be helpful to everyone in PHP programming. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn