Đang chuẩn bị liên kết để tải về tài liệu:
PHP 5 Recipes A Problem-Solution Approach PHẦN 5
Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Ví dụ, mỗi thể hiện của Parrot có một birdCall (). Bởi vì birdCall () là một phương pháp nào, bạn có thể định nghĩa lại nó trong Parrot mà không ảnh hưởng đến phương pháp birdCall () khi được gọi là một thể hiện của chim hoặc phân lớp khác. Đây là những gì chúng tôi có ý nghĩa trọng một phương pháp của một lớp cha mẹ. | 250 5-13 EXTENDING THE DATE CLASS Date is now Mon 26 Feb 2007 21 30 45 1000. Using the toString method on the same date yields 2007-02-26 21 30 45 10 00. Finally let s try some other ways to call the constructor. First the RFC-formatted date 24 Sept 2005 2005-09-24 00 00 00 10 00. And now we ll try it with xmas2k new Date 2000 11 25 followed by echo xmas2k- toLocaleString echo xmas2k- toUTCString Mon 25 Dec 2000 00 00 00 1000 Sun 24 Dec 2000 14 00 00 UTC Now for some UTC methods using xmas2k. Calling xmas2k- setUTCDate 30 UTC date Sat 30 Dec 2000 14 00 00 UTC local date Sun 31 Dec 2000 00 00 00 1000 Calling xmas2k- setUTCHours 48 UTC date Mon 01 Jan 2001 00 00 00 UTC local date Mon 1 Jan 2001 10 00 00 1000 Calling xmas2k- setUTCFullYear 2008 UTC date Tue 01 Jan 2008 00 00 00 UTC local date Tue 1 Jan 2008 10 00 00 1000 This may seem like a lot of work just to get and set dates but the payoff comes from having a clear and well-defined interface for dealing with a complex structure. However so far you are able to use this class to work with dates only as numbers or sets of numbers. What about being able to output dates in a more conventional format such as March 15 2005 8 25 p.m. Do you have to sacrifice PHP s capabilities in this regard in order to have a nice programming construct The answer to this question is No you do not. As you saw in Chapter 2 it is possible to extend classes to provide additional functionality. This is exactly what you are going to do in the next example. 5-13. Extending the Date Class PHP s native date and time functions provide lots of output options which the Date class you developed in the previous section does not really let you take advantage of. You could go back and add new methods to that class to take care of this shortcoming but you do not really want to do this for a couple of reasons The Date class having more than two dozen methods is already fairly long and complex. Any advantage you might gain from adding methods to it is .