Detailed explanation of object-oriented inheritance usage in PHP

墨辰丷
Release: 2023-03-28 12:14:01
Original
1191 people have browsed it

This article mainly introduces the use of object-oriented inheritance in PHP, and analyzes the use of inheritance in object-oriented programming in PHP with examples and related operating techniques for code optimization and reducing code duplication. Friends in need can refer to the following

The examples in this article describe the use of object-oriented inheritance in PHP. Share it with everyone for your reference, the details are as follows:

Inheritance

Let’s look at the two classes first


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"; ?>
Copy after login


Output:

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

Comments: The code duplication of these two categories is too high, there are similarities and differences. It's better to use inheritance to simplify the process.

Use inheritance to process


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"; ?>
Copy after login


Output:

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

Comments: Inheritance processing solves differences and compatibility very well question.

Further optimization


##

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"; ?>
Copy after login


Output:

cd1 (bobbleson, bob): playing time - 50

book1 (harrelson, harry): page count - 30

Comment: Here the common attributes are placed in the parent class, and other personal attributes are placed Handle it in your own class. And set your own construction method and inherit the construction method of the parent class.

Further inherited methods from the parent class


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"; ?>
Copy after login


Output :

cd1 (bobbleson, bob): playing time - 50

book1 (harrelson, harry): page count - 30

Comments: The same result can be optimized and optimized again. Here the methods of the parent class are inherited. parent::getSummaryLine(). However, this is used less frequently.

Continue to add some interesting content


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"; ?>
Copy after login


##Output :

cd1 (bobbleson, bob): playing time - 50

price: 1

book1 (harrelson, harry): page count - 30
price: 4

Comment: The parent class added a discount. After book inherited it, the getPrice method was modified, so the discount is not valid for book.

Private properties, set and get through methods

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"; ?>
Copy after login


Output:

cd1 (bobbleson, bob): playing time - 50

book1 (harrelson, harry): page count - 30


Comments: The attributes are further privatized here , can only be obtained through methods. This ensures safety.


The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

php file search


PHPLocal development environment docker installation

PHPEncapsulation database addition, deletion, modification and query

The above is the detailed content of Detailed explanation of object-oriented inheritance usage in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!