PHP Object-Oriented Solutions phần 3

và sau đó săn bắn cho các dấu chấm phẩy lừa đảo. Mã tự xem xét: Điều này là thực sự quan trọng khi làm việc với OOP. IDE theo dõi của người dùng định nghĩa các lớp học, các biến, và các chức năng trong dự án hiện tại và cho phép hoàn thành mã cho họ, quá. Hình 1-4 cho thấy mã các gợi ý được tạo ra bởi PhpED cho lớp Pos_Validator trong Chương 4. | Simpo PDF Merge and Split Unregistered Version - http PHP OBJECT-ORIENTED SOLUTIONS This includes the class files for the Ch2_Book and Ch2_ShoppingCart classes creates an instance of each class and adds the book object to the cart. As explained earlier the exercises refer to individual files by a basic name. If you have been doing the exercises yourself should contain the correct code. However the class definition has changed several times throughout the chapter so the download files use numbers at the end of each filename to ensure the correct one is loaded. 3. Test the page and you should see the result shown in Figure 2-10. No surprises book is a Ch2_Book object and that s what the type hint in Ch2_ShoppingCart addItem specifies. Figure 2-10. The book has been added to the cart. 4. Now let s try adding a DVD to the cart. You need to include the Ch2_DVD class file and create an instance of the class. Amend the code in like this it s in require_once . Ch2 require_once . Ch2 require_once . Ch2 book new Ch2_Book PHP Object-Oriented Solutions 300 cart new Ch2_ShoppingCart cart- addItem book movie new Ch2_DVD Atonement 2 hr 10 min cart- addItem movie 5. Test the page again. This time you should see the error of your ways well mine since I told you to do it as shown in Figure 2-11. Figure 2-11. The DVD is of the wrong data type and generates a fatal error. 58 Simpo PDF Merge and Split Unregistered Version - http WRITING PHP CLASSES 6. To fix the problem change the type hint in Ch2_ShoppingCart addItem like this the code is in public function addItem Ch2_Product item 7. Test the page again the download file is because it needs to include the amended Ch2_ShoppingCart class . This time both items are accepted as shown in Figure 2-12 because the addItem method expects an object of their parent class Ch2_Product. 2 Figure 2-12. .

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
5    97    2    21-05-2024
8    67    2    21-05-2024
284    82    3    21-05-2024
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.