PHP Objects, Patterns, and Practice- P2
Số trang: 50
Loại file: pdf
Dung lượng: 330.47 KB
Lượt xem: 8
Lượt tải: 0
Xem trước 5 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
PHP Objects, Patterns, and Practice- P2: This book takes you beyond the PHP basics to the enterprise development practices used by professional programmers. Updated for PHP 5.3 with new sections on closures, namespaces, and continuous integration, this edition will teach you about object features such as abstract classes, reflection, interfaces, and error handling. You’ll also discover object tools to help you learn more about your classes, objects, and methods.
Nội dung trích xuất từ tài liệu:
PHP Objects, Patterns, and Practice- P2 CHAPTER 3 ■ OBJECT BASICS function getSummaryLine() { $base = {$this->title} ( {$this->producerMainName}, ; $base .= {$this->producerFirstName} ); if ( $this->type == book ) { $base .= : page count - {$this->numPages}; } else if ( $this->type == cd ) { $base .= : playing time - {$this->playLength}; } return $base; } In order to set the $type property, I could test the $numPages argument to the constructor. Still, onceagain, the ShopProduct class has become more complex than necessary. As I add more differences to myformats, or add new formats, these functional differences will become even harder to manage. Perhaps Ishould try another approach to this problem. Since ShopProduct is beginning to feel like two classes in one, I could accept this and create twotypes rather than one. Here’s how I might do it: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 { 29 CHAPTER 3 ■ OBJECT BASICS 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}; } } I have addressed the complexity issue, but at a cost. I can now create a getSummaryLine() method for each format without having to test a flag. Neither class maintains fields or methods that are not relevant to it. The cost lies in duplication. The getProducerName() method is exactly the same in each class. Each constructor sets a number of identical properties in the same way. This is another unpleasant odor you should train yourself to sniff out. If I need the getProducer() methods to behave identically for each class, any changes I make to one implementation will need to be made for the other. Without care, the classes will soon slip out of synchronization. Even if I am confident that I can maintain the duplication, my worries are not over. I now have two types rather than one. Remember the ShopProductWriter class? Its write() method is designed to work with a single type: ShopProduct. How can I amend this to work as before? I could remove the class type hint from the method declaration, but then I must trust to luck that write() is passed an object of the correct type. I could add my own type checking code to the body of the method: class ShopProductWriter { public function write( $shopProduct ) { if ( ! ( $shopProduct instanceof CdProduct ) && ! ( $shopProduct instanceof BookProduct ) ) { die( wrong type supplied );30 CHAPTER 3 ■ OBJECT BASICS } ...
Nội dung trích xuất từ tài liệu:
PHP Objects, Patterns, and Practice- P2 CHAPTER 3 ■ OBJECT BASICS function getSummaryLine() { $base = {$this->title} ( {$this->producerMainName}, ; $base .= {$this->producerFirstName} ); if ( $this->type == book ) { $base .= : page count - {$this->numPages}; } else if ( $this->type == cd ) { $base .= : playing time - {$this->playLength}; } return $base; } In order to set the $type property, I could test the $numPages argument to the constructor. Still, onceagain, the ShopProduct class has become more complex than necessary. As I add more differences to myformats, or add new formats, these functional differences will become even harder to manage. Perhaps Ishould try another approach to this problem. Since ShopProduct is beginning to feel like two classes in one, I could accept this and create twotypes rather than one. Here’s how I might do it: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 { 29 CHAPTER 3 ■ OBJECT BASICS 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}; } } I have addressed the complexity issue, but at a cost. I can now create a getSummaryLine() method for each format without having to test a flag. Neither class maintains fields or methods that are not relevant to it. The cost lies in duplication. The getProducerName() method is exactly the same in each class. Each constructor sets a number of identical properties in the same way. This is another unpleasant odor you should train yourself to sniff out. If I need the getProducer() methods to behave identically for each class, any changes I make to one implementation will need to be made for the other. Without care, the classes will soon slip out of synchronization. Even if I am confident that I can maintain the duplication, my worries are not over. I now have two types rather than one. Remember the ShopProductWriter class? Its write() method is designed to work with a single type: ShopProduct. How can I amend this to work as before? I could remove the class type hint from the method declaration, but then I must trust to luck that write() is passed an object of the correct type. I could add my own type checking code to the body of the method: class ShopProductWriter { public function write( $shopProduct ) { if ( ! ( $shopProduct instanceof CdProduct ) && ! ( $shopProduct instanceof BookProduct ) ) { die( wrong type supplied );30 CHAPTER 3 ■ OBJECT BASICS } ...
Tìm kiếm theo từ khóa liên quan:
thủ thuật lập trình lập trình php lập trình web giáo trình cơ bản lập trình html ngôn ngữ javaGợi ý tài liệu liên quan:
-
Thủ thuật giúp giải phóng dung lượng ổ cứng
4 trang 210 0 0 -
NGÂN HÀNG CÂU HỎI TRẮC NGHIỆM THIẾT KẾ WEB
8 trang 202 0 0 -
Bài toán phân luồng giao thông và ứng dụng
11 trang 179 1 0 -
Hướng dẫn lập trình với Android part 4
5 trang 154 0 0 -
[Thảo luận] Học PHP như thế nào khi bạn chưa biết gì về lập trình?
5 trang 131 0 0 -
161 trang 129 1 0
-
142 trang 129 0 0
-
Bài giảng Lập trình web nâng cao: Chương 8 - Trường ĐH Văn Hiến
36 trang 109 1 0 -
MỘT SỐ ĐIỂM CẦN CHÚ Ý KHI THIẾT KẾ WEB
5 trang 108 0 0 -
150 trang 103 0 0