TAILIEUCHUNG - OBJECT-ORIENTED PHP Concepts, Techniques, and Code- P11

OBJECT-ORIENTED PHP Concepts, Techniques, and Code- P11: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. | Overridden Methods Listing 10-3 shows all the code required to create a class derived from Exception. There are only two methods and both are overridden parent class methods. But let s take a more detailed look beginning with the constructor. Note how it checks the value of the error number. This test is designed to separate errors attributable to the programmer from all other errors. We ve chosen the range 5 000 and greater because this range is not used by built-in MySQL errors. The message associated with programmer errors indicates misuse of the class and differentiating client programmer errors from other errors makes it easier to use the database classes. For clarity the error message includes the class name which we avoid hard-coding by using the constant__CLASS_. After identifying the type of error the Exception class constructor is called using the scope resolution operator and the keyword parent. You encountered similar syntax when you referenced a static variable in Chapter 9. This is the syntax for calling any parent method from within a derived class and one of the few cases where it s necessary to invoke a magic method directly. As you can see there is no need to hard-code the parent class name because all constructors are invoked by calling_construct the very reason for introducing a magic construction method in PHP 5. NOTE If a derived class overrides a parent constructor there is no implicit call to the parent. The call must be made explicitly as in Listing 10-3. ThetoString method defined in Listing 10-3 replaces the_toString method inherited from the parent class. As a result a MySQLException echoed to the screen shows only the error number and the associated message which is much less informative than the__toString method of the parent class which traces the error and shows its line number . This makes for more secure production code because it reduces the information associated with an exception but it also makes development of applications