当前位置: 首页 > 编程笔记 >

PHP面向对象继承用法详解(优化与减少代码重复)

吴均
2023-03-14
本文向大家介绍PHP面向对象继承用法详解(优化与减少代码重复),包括了PHP面向对象继承用法详解(优化与减少代码重复)的使用技巧和注意事项,需要的朋友参考一下

本文实例讲述了PHP面向对象继承用法。分享给大家供大家参考,具体如下:

继承

先看两个类

<?php
class CdProduct {
  public $playLength; // 播放时间
  public $title;
  public $producerMainName;
  public $producerFirstName;
  public $price;
  function __construct(  $title, $firstName,
              $mainName, $price,
              $playLength ) {
    $this->title       = $title;
    $this->producerFirstName = $firstName;
    $this->producerMainName = $mainName;
    $this->price       = $price;
    $this->playLength    = $playLength;
  }
  function getPlayLength() {
    return $this->playLength;
  }
  function getSummaryLine() {
    $base = "{$this->title} ( {$this->producerMainName}, ";
    $base .= "{$this->producerFirstName} )";
    $base .= ": playing time - {$this->playLength}";
    return $base;
  }
  function getProducer() {
    return "{$this->producerFirstName}".
        " {$this->producerMainName}";
  }
}
class BookProduct {
  public $numPages; // 看的页数
  public $title;
  public $producerMainName;
  public $producerFirstName;
  public $price;
  function __construct(  $title, $firstName,
              $mainName, $price,
              $numPages ) {
    $this->title       = $title;
    $this->producerFirstName = $firstName;
    $this->producerMainName = $mainName;
    $this->price       = $price;
    $this->numPages     = $numPages;
  }
  function getNumberOfPages() {
    return $this->numPages;
  }
  function getSummaryLine() {
    $base = "{$this->title} ( {$this->producerMainName}, ";
    $base .= "{$this->producerFirstName} )";
    $base .= ": page count - {$this->numPages}";
    return $base;
  }
  function getProducer() {
    return "{$this->producerFirstName}".
        " {$this->producerMainName}";
  }
}
$product1 = new CdProduct("cd1", "bob", "bobbleson", 4, 50 );
print $product1->getSummaryLine();
print "\n";
$product2 = new BookProduct("book1", "harry", "harrelson", 4, 30 );
print $product2->getSummaryLine();
print "\n";
?>

输出:

cd1 ( bobbleson, bob ): playing time - 50
book1 ( harrelson, harry ): page count - 30

点评:这两个类,代码重复性太高,有相同性,也有差异性。不如用继承来简化处理。

采用继承来处理

<?php
class ShopProduct {
  public $numPages;
  public $playLength;
  public $title;
  public $producerMainName;
  public $producerFirstName;
  public $price;
  function __construct(  $title, $firstName,
              $mainName, $price,
              $numPages=0, $playLength=0 ) {
    $this->title       = $title;
    $this->producerFirstName = $firstName;
    $this->producerMainName = $mainName;
    $this->price       = $price;
    $this->numPages     = $numPages;
    $this->playLength    = $playLength;
  }
  function getProducer() {
    return "{$this->producerFirstName}".
        " {$this->producerMainName}";
  }
  function getSummaryLine() {
    $base = "$this->title ( {$this->producerMainName}, ";
    $base .= "{$this->producerFirstName} )";
    return $base;
  }
}
class CdProduct extends ShopProduct {
  function getPlayLength() { // 增加属于自己的方法
    return $this->playLength;
  }
  function getSummaryLine() { // 改造了父类的方法
    $base = "{$this->title} ( {$this->producerMainName}, ";
    $base .= "{$this->producerFirstName} )";
    $base .= ": playing time - {$this->playLength}";
    return $base;
  }
}
class BookProduct extends ShopProduct {
  function getNumberOfPages() {
    return $this->numPages;
  }
  function getSummaryLine() {
    $base = "{$this->title} ( {$this->producerMainName}, ";
    $base .= "{$this->producerFirstName} )";
    $base .= ": page count - {$this->numPages}";
    return $base;
  }
}
$product1 = new CdProduct("cd1", "bob", "bobbleson", 4, null, 50 );
print $product1->getSummaryLine();
print "\n";
$product2 = new BookProduct("book1", "harry", "harrelson", 4, 30 );
print $product2->getSummaryLine();
print "\n";
?>

输出:

cd1 ( bobbleson, bob ): playing time - 50
book1 ( harrelson, harry ): page count - 30

点评:继承处理很好的解决了差异性,相通性问题。

进一步优化处理

<?php
class ShopProduct {
  // 抽离出共有属性
  public $title;
  public $producerMainName;
  public $producerFirstName;
  public $price;
  function __construct(  $title, $firstName,
              $mainName, $price ) {
    $this->title       = $title;
    $this->producerFirstName = $firstName;
    $this->producerMainName = $mainName;
    $this->price       = $price;
  }
  function getProducer() {
    return "{$this->producerFirstName}".
        " {$this->producerMainName}";
  }
  function getSummaryLine() {
    $base = "{$this->title} ( {$this->producerMainName}, ";
    $base .= "{$this->producerFirstName} )";
    return $base;
  }
}
class CdProduct extends ShopProduct {
  // 抽离出属于自己特有的属性
  public $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 = "{$this->title} ( {$this->producerMainName}, ";
    $base .= "{$this->producerFirstName} )";
    $base .= ": playing time - {$this->playLength}";
    return $base;
  }
}
class BookProduct extends ShopProduct {
  public $numPages;
  function __construct(  $title, $firstName,
              $mainName, $price, $numPages ) {
    parent::__construct(  $title, $firstName,
                $mainName, $price );
    $this->numPages = $numPages;
  }
  function getNumberOfPages() {
    return $this->numPages;
  }
  function getSummaryLine() {
    $base = "$this->title ( $this->producerMainName, ";
    $base .= "$this->producerFirstName )";
    $base .= ": page count - $this->numPages";
    return $base;
  }
}
$product1 = new CdProduct("cd1", "bob", "bobbleson", 4, 50 );
print $product1->getSummaryLine();
print "\n";
$product2 = new BookProduct("book1", "harry", "harrelson", 4, 30 );
print $product2->getSummaryLine();
print "\n";
?>

输出:

cd1 ( bobbleson, bob ): playing time - 50
book1 ( harrelson, harry ): page count - 30

点评:这里把共有属性在父类中,其他个性属性放在自己的类中处理。并设置自己的构造方法,继承父类的构造方法。

进一步继承父类的方法

<?php
class ShopProduct {
  public $title;
  public $producerMainName;
  public $producerFirstName;
  public $price;
  function __construct(  $title, $firstName,
              $mainName, $price ) {
    $this->title       = $title;
    $this->producerFirstName = $firstName;
    $this->producerMainName = $mainName;
    $this->price       = $price;
  }
  function getProducer() {
    return "{$this->producerFirstName}".
        " {$this->producerMainName}";
  }
  function getSummaryLine() {
    $base = "{$this->title} ( {$this->producerMainName}, ";
    $base .= "{$this->producerFirstName} )";
    return $base;
  }
}
class CdProduct extends ShopProduct {
  public $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 {
  public $numPages;
  function __construct(  $title, $firstName,
              $mainName, $price, $numPages ) {
    parent::__construct(  $title, $firstName,
                $mainName, $price );
    $this->numPages = $numPages;
  }
  function getNumberOfPages() {
    return $this->numPages;
  }
  function getSummaryLine() {
    $base = parent::getSummaryLine();
    $base .= ": page count - {$this->numPages}";
    return $base;
  }
}
$product1 = new CdProduct("cd1", "bob", "bobbleson", 4, 50 );
print $product1->getSummaryLine();
print "\n";
$product2 = new BookProduct("book1", "harry", "harrelson", 4, 30 );
print $product2->getSummaryLine();
print "\n";
?>

输出:

cd1 ( bobbleson, bob ): playing time - 50
book1 ( harrelson, harry ): page count - 30

点评:同样的结果,可以优化优化再优化。这里继承父类的方法。parent::getSummaryLine()。不过这个用的比较少。

继续添加一些有意思的内容

<?php
class ShopProduct {
  private $title;
  private $discount = 0;
  private $producerMainName;
  private $producerFirstName;
  protected $price;
  function __construct(  $title, $firstName,
              $mainName, $price ) {
    $this->title       = $title;
    $this->producerFirstName = $firstName;
    $this->producerMainName = $mainName;
    $this->price       = $price;
  }
  function setDiscount( $num ) {
    $this->discount=$num;
  }
  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 {
  public $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 {
  public $numPages;
  function __construct(  $title, $firstName,
              $mainName, $price, $numPages ) {
    parent::__construct(  $title, $firstName,
                $mainName, $price );
    $this->numPages = $numPages;
  }
  function getPrice() {
    return $this->price;
  }
  function getNumberOfPages() {
    return $this->numPages;
  }
  function getSummaryLine() {
    $base = parent::getSummaryLine();
    $base .= ": page count - {$this->numPages}";
    return $base;
  }
}
$product1 = new CdProduct("cd1", "bob", "bobbleson", 4, 50 );
$product1->setDiscount( 3 );
print $product1->getSummaryLine();
print "\n";
print "price: {$product1->getPrice()}\n";
$product2 = new BookProduct("book1", "harry", "harrelson", 4, 30 );
$product2->setDiscount( 3 ); // 折扣对book无效
print $product2->getSummaryLine();
print "\n";
print "price: {$product2->getPrice()}\n";
?>

输出:

cd1 ( bobbleson, bob ): playing time - 50
price: 1
book1 ( harrelson, harry ): page count - 30
price: 4

点评:父类添加了折扣,book继承之后,修改了getPrice方法,所以折扣对book无效。

私有化属性,通过方法来设置与获取

<?php
class ShopProduct {
  // 私有化属性,通过方法来设置与获取
  private $title;
  private $producerMainName;
  private $producerFirstName;
  protected $price;
  private $discount = 0;
  public function __construct(  $title, $firstName,
              $mainName, $price ) {
    $this->title       = $title;
    $this->producerFirstName = $firstName;
    $this->producerMainName = $mainName;
    $this->price       = $price;
  }
  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);
  }
  public function getProducer() {
    return "{$this->producerFirstName}".
        " {$this->producerMainName}";
  }
  public function getSummaryLine() {
    $base = "{$this->title} ( {$this->producerMainName}, ";
    $base .= "{$this->producerFirstName} )";
    return $base;
  }
}
class CdProduct extends ShopProduct {
  private $playLength = 0;
  public function __construct(  $title, $firstName,
              $mainName, $price, $playLength ) {
    parent::__construct(  $title, $firstName,
                $mainName, $price );
    $this->playLength = $playLength;
  }
  public function getPlayLength() {
    return $this->playLength;
  }
  public function getSummaryLine() {
    $base = parent::getSummaryLine();
    $base .= ": playing time - {$this->playLength}";
    return $base;
  }
}
class BookProduct extends ShopProduct {
  private $numPages = 0;
  public function __construct(  $title, $firstName,
              $mainName, $price, $numPages ) {
    parent::__construct(  $title, $firstName,
                $mainName, $price );
    $this->numPages = $numPages;
  }
  public function getNumberOfPages() {
    return $this->numPages;
  }
  public function getSummaryLine() {
    $base = parent::getSummaryLine();
    $base .= ": page count - {$this->numPages}";
    return $base;
  }
  public function getPrice() {
    return $this->price;
  }
}
$product1 = new CdProduct("cd1", "bob", "bobbleson", 4, 50 );
print $product1->getSummaryLine()."\n";
$product2 = new BookProduct("book1", "harry", "harrelson", 4, 30 );
print $product2->getSummaryLine()."\n";
?>

输出:

cd1 ( bobbleson, bob ): playing time - 50
book1 ( harrelson, harry ): page count - 30

点评:这里进一步私有化了属性,要想获取只能通过方法。这样就确保了安全性。

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php面向对象程序设计入门教程》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

 类似资料:
  • 本文向大家介绍JS 面向对象之继承---多种组合继承详解,包括了JS 面向对象之继承---多种组合继承详解的使用技巧和注意事项,需要的朋友参考一下 这一次要讲 组合、原型式、寄生式、寄生组合式继承方式。 1. 组合继承:又叫伪经典继承,是指将原型链和借用构造函数技术组合在一块的一种继承方式。 下面来看一个例子: 组合继承避免了原型链和借用构造函数的缺陷,融合它们的优点。 2. 原型式继承 可以在不

  • 本文向大家介绍PHP Trait代码复用类与多继承实现方法详解,包括了PHP Trait代码复用类与多继承实现方法详解的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了PHP Trait代码复用类与多继承实现方法。分享给大家供大家参考,具体如下: 前言 众所周知,一直以来PHP和很多语言一样是单继承的语言,但是常常在编码过程中,我们需要在当前类中使用两个或两个以上的其他类的方法,这种情况下继

  • 本文向大家介绍Python面向对象程序设计之继承、多态原理与用法详解,包括了Python面向对象程序设计之继承、多态原理与用法详解的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了Python面向对象程序设计之继承、多态原理与用法。分享给大家供大家参考,具体如下: 继承:多继承、super、__init__、重写父类变量或函数 多态 继承: 在Python3中,不写基类的类默认继承objec

  • 本文向大家介绍php使用类继承解决代码重复的问题,包括了php使用类继承解决代码重复的问题的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了php使用类继承解决代码重复的问题。分享给大家供大家参考。具体分析如下: 继承直白地说就是给一个类建一个或多个子类,要创建子类就必须在类声明中使用 extends 关键字,新类名在前,extends 在中,父类名在后。   下例中,我们创建两个新类,Bo

  • 本文向大家介绍浅谈JavaScript面向对象--继承,包括了浅谈JavaScript面向对象--继承的使用技巧和注意事项,需要的朋友参考一下 一、继承的概念 子类共享父类的数据和方法的行为,就叫继承。 二、E55如何实现继承?探索JavaScript继承的本质 2.1构造函数之间的“复制粘贴” 第一条路是通过构造函数来继承,这可以理解为子类直接复制了父类构造函数的代码,然后按照一定的规矩“粘贴”

  • typora-copy-images-to: img 继承的描述 在现实生活中,继承一般指的是子女继承父辈的财产。在程序中,继承描述的是事物之间的所属关系,通过继承可以使多种事物之间形成一种关系体系。例如猫和狗都属于动物,程序中便可以描述为猫和狗继承自动物,同理,波斯猫和巴厘猫继承自猫,而沙皮狗和斑点狗继承自狗。这些动物之间会形成一个继承体系,具体如下图所示。 在Java中,类的继承是指在一个现有