![Phân tích tư tưởng của nhân dân qua đoạn thơ: Những người vợ nhớ chồng… Những cuộc đời đã hóa sông núi ta trong Đất nước của Nguyễn Khoa Điềm](https://timtailieu.net/upload/document/136415/phan-tich-tu-tuong-cua-nhan-dan-qua-doan-tho-039-039-nhung-nguoi-vo-nho-chong-nhung-cuoc-doi-da-hoa-song-nui-ta-039-039-trong-dat-nuoc-cua-nguyen-khoa-136415.jpg)
PHP Objects, Patterns, and Practice- P7
Số trang: 50
Loại file: pdf
Dung lượng: 598.33 KB
Lượt xem: 17
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- P7: 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- P7 CHAPTER 13 ■ DATABASE PATTERNS return $obj; } protected function doInsert( woodomainDomainObject $object ) { print inserting ; debug_print_backtrace(); $values = array( $object->getName() ); $this->insertStmt->execute( $values ); $id = self::$PDO->lastInsertId(); $object->setId( $id ); } function update( woodomainDomainObject $object ) { print updating ; $values = array( $object->getName(), $object->getId(), $object->getId() ); $this->updateStmt->execute( $values ); } function selectStmt() { return $this->selectStmt; }} Once again, this class is stripped of some of the goodies that are still to come. Nonetheless, it doesits job. The constructor prepares some SQL statements for use later on. These could be made static andshared across VenueMapper instances, or as described earlier, a single Mapper object could be stored in aRegistry, thereby saving the cost of repeated instantiation. These are refactorings I will leave to you! The Mapper class implements find(), which invokes selectStmt() to acquire the prepared SELECTstatement. Assuming all goes well, Mapper invokes VenueMapper::doCreateObject(). It’s here that I usethe associative array to generate a Venue object. From the point of view of the client, this process is simplicity itself:$mapper = new woomapperVenueMapper();$venue = $mapper->find( 12 );print_r( $venue ); The print_r() method is a quick way of confirming that find() was successful. In my system (wherethere is a row in the venue table with ID 12), the output from this fragment is as follows:woodomainVenue Object( [name:woodomainVenue:private] => The Eyeball Inn [spaces:woodomainVenue:private] => [id:woodomainDomainObject:private] => 12) The doInsert() and update() methods reverse the process established by find(). Each accepts aDomainObject, extracts row data from it, and calls PDOStatement::execute() with the resultinginformation. Notice that the doInsert() method sets an ID on the provided object. Remember thatobjects are passed by reference in PHP, so the client code will see this change via its own reference. Another thing to note is that doInsert() and update() are not really type safe. They will accept anyDomainObject subclass without complaint. You should perform an instanceof test and throw anException if the wrong object is passed. This will guard against the inevitable bugs. 279 CHAPTER 13 ■ DATABASE PATTERNS Once again, here is a client perspective on inserting and updating: $venue = new woodomainVenue(); $venue->setName( The Likey Lounge-yy ); // add the object to the database $mapper->insert( $venue ); // find the object again – just prove it works! $venue = $mapper->find( $venue->getId() ); print_r( $venue ); // alter our object $venue->setName( The Bibble Beer Likey Lounge-yy ); // call update to enter the amended data $mapper->update( $venue ); // once again, go back to the database to prove it worked $venue = $mapper->find( $venue->getId() ); print_r( $venue ); Handling Multiple Rows The find() method is pretty straightforward, because it only needs to return a single object. What do you do, though, if you need to pull lots of data from the database? Your first thought may be to return an array of objects. This will work, but there is a major problem with the approach. If you return an array, each object in the collection will need to be instantiated first, which, if you have a result set of 1,000 objects, may be needlessly expensive. An alternative would be to simply return an array and let the calling code sort out object instantiation. This is possible, but it violates the very purpose of the Mapper classes. There is one way you can have your cake and eat it. You can use the built-in Iterator interface. The Iterator interface requires implementing classes to define methods for querying a list. If you do this, your class can be used in foreach loops just like an array. There are some people who say that iterator implementations are unnecessary in a language like PHP with such good support for arrays. Tish and piffle! I will show you at least three good reasons for using PHP’s built-in Iterator interface in this chapter. Table 13–1 shows the methods that the Iterator interface requires. Table 13–1. Methods Defined b ...
Nội dung trích xuất từ tài liệu:
PHP Objects, Patterns, and Practice- P7 CHAPTER 13 ■ DATABASE PATTERNS return $obj; } protected function doInsert( woodomainDomainObject $object ) { print inserting ; debug_print_backtrace(); $values = array( $object->getName() ); $this->insertStmt->execute( $values ); $id = self::$PDO->lastInsertId(); $object->setId( $id ); } function update( woodomainDomainObject $object ) { print updating ; $values = array( $object->getName(), $object->getId(), $object->getId() ); $this->updateStmt->execute( $values ); } function selectStmt() { return $this->selectStmt; }} Once again, this class is stripped of some of the goodies that are still to come. Nonetheless, it doesits job. The constructor prepares some SQL statements for use later on. These could be made static andshared across VenueMapper instances, or as described earlier, a single Mapper object could be stored in aRegistry, thereby saving the cost of repeated instantiation. These are refactorings I will leave to you! The Mapper class implements find(), which invokes selectStmt() to acquire the prepared SELECTstatement. Assuming all goes well, Mapper invokes VenueMapper::doCreateObject(). It’s here that I usethe associative array to generate a Venue object. From the point of view of the client, this process is simplicity itself:$mapper = new woomapperVenueMapper();$venue = $mapper->find( 12 );print_r( $venue ); The print_r() method is a quick way of confirming that find() was successful. In my system (wherethere is a row in the venue table with ID 12), the output from this fragment is as follows:woodomainVenue Object( [name:woodomainVenue:private] => The Eyeball Inn [spaces:woodomainVenue:private] => [id:woodomainDomainObject:private] => 12) The doInsert() and update() methods reverse the process established by find(). Each accepts aDomainObject, extracts row data from it, and calls PDOStatement::execute() with the resultinginformation. Notice that the doInsert() method sets an ID on the provided object. Remember thatobjects are passed by reference in PHP, so the client code will see this change via its own reference. Another thing to note is that doInsert() and update() are not really type safe. They will accept anyDomainObject subclass without complaint. You should perform an instanceof test and throw anException if the wrong object is passed. This will guard against the inevitable bugs. 279 CHAPTER 13 ■ DATABASE PATTERNS Once again, here is a client perspective on inserting and updating: $venue = new woodomainVenue(); $venue->setName( The Likey Lounge-yy ); // add the object to the database $mapper->insert( $venue ); // find the object again – just prove it works! $venue = $mapper->find( $venue->getId() ); print_r( $venue ); // alter our object $venue->setName( The Bibble Beer Likey Lounge-yy ); // call update to enter the amended data $mapper->update( $venue ); // once again, go back to the database to prove it worked $venue = $mapper->find( $venue->getId() ); print_r( $venue ); Handling Multiple Rows The find() method is pretty straightforward, because it only needs to return a single object. What do you do, though, if you need to pull lots of data from the database? Your first thought may be to return an array of objects. This will work, but there is a major problem with the approach. If you return an array, each object in the collection will need to be instantiated first, which, if you have a result set of 1,000 objects, may be needlessly expensive. An alternative would be to simply return an array and let the calling code sort out object instantiation. This is possible, but it violates the very purpose of the Mapper classes. There is one way you can have your cake and eat it. You can use the built-in Iterator interface. The Iterator interface requires implementing classes to define methods for querying a list. If you do this, your class can be used in foreach loops just like an array. There are some people who say that iterator implementations are unnecessary in a language like PHP with such good support for arrays. Tish and piffle! I will show you at least three good reasons for using PHP’s built-in Iterator interface in this chapter. Table 13–1 shows the methods that the Iterator interface requires. Table 13–1. Methods Defined b ...
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ữ javaTài liệu liên quan:
-
NGÂN HÀNG CÂU HỎI TRẮC NGHIỆM THIẾT KẾ WEB
8 trang 218 0 0 -
Thủ thuật giúp giải phóng dung lượng ổ cứng
4 trang 217 0 0 -
Bài toán phân luồng giao thông và ứng dụng
11 trang 181 1 0 -
Hướng dẫn lập trình với Android part 4
5 trang 156 0 0 -
161 trang 134 1 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 134 0 0 -
142 trang 130 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 121 1 0 -
MỘT SỐ ĐIỂM CẦN CHÚ Ý KHI THIẾT KẾ WEB
5 trang 115 0 0 -
GIÁO TRÌNH LẬP TRÌNH WEB_PHẦN 2_BÀI 3
3 trang 105 0 0