TAILIEUCHUNG - OBJECT-ORIENTED PHP Concepts, Techniques, and Code- P7
OBJECT-ORIENTED PHP Concepts, Techniques, and Code- P7:A number of years ago, before I started using PHP, I created dynamic web pages using C. This really wasn’t too different from some of the other options available at the time, though it seems almost unthinkable now. Creating a dynamic page meant outputting HTML from your script and recompiling that script if any changes needed to be made. | Public Methods Following the constructor is another public method with the namedestruct. This method is known as a destructor. The double underscore __ in front of the function name indicates that this method like the constructor is another magic method. Again it is a method newly introduced in PHP 5. Recall from Chapter 5 that magic methods happen in the background like magic. public function __destruct if isset this- image imagedestroy this- image While the use of destructors is new with PHP 5 anyone familiar with other OO languages has probably already come across them. As its name suggests a destructor is the opposite of a constructor. A constructor initializes an object while a destructor frees up resources that a class may have allocated. Generally PHP does a good job of cleaning up after itself so destructors are often not strictly necessary. It s used here to ensure that the image resource is disposed of. Garbage Collection Like Java PHP employs a garbage collector to automatically clean up resources. Because the programmer is not responsible for allocating and freeing memory as he is in a language like C for example an automated program must free up resources. The garbage collector determines when objects are no longer used and then disposes of them for example when they go out of scope. However using destructors can act as a cue that speeds up garbage collection. In the case of the Thumbnailimage class the destructor disposes of the reduced image copy by calling the imagedestroy function. You can call the destructor directly but as with other magic methods the destructor is designed to be invoked in the background without any intervention by the programmer. Displaying the Image Next to the constructor getimage see Listing 6-4 is the most important method in the Thumbnailimage class because it actually sends a reduced image to the browser. Its logic is simple A header is sent to the browser announcing what to expect followed by the appropriate .
đang nạp các trang xem trước