Preliminary PHP5 (2)
Author: Luis Argerich translation: Avenger Source: PHPBuild abstract class abstract class can not be instantiated. Abstract classes are the same as other classes, allowing definition variables and methods. Abstract classes can also define an abstract way, and the abstract class method will not be executed, but it is possible to perform in its derived class. Example 6: Abstract class Phpabstract class foo {protected $ x; Abstract Function Display (); Function SetX ($ x) {$ this-> x = $ x;}}} class foo2 extends foo {function display () {/ / Code}}?> __Call php5 has added a dedicated method __call (), which is used to monitor other methods in an object. If you try to call a method that does not exist in an object, the __ call method will be called automatically. Example 7: __ call Phpclass foo {function __call ($ name, $ arguments) {print ("DID You Call Me? I'm $ Name!");}} $ X = new foo (); $ x-> Dostuff (); $ x-> fancy_stuff ();> This special method can be used to implement the "Overloading" action so you can check your parameters and pass the parameters by calling a private method. . Example 8: Using __call to implement "overload" action Phpclass magic {function __call ($ name, $ arguments) {if ($ name == 'foo') {if (is_int ($ arguments [0])) $ THIS -> foo_for_int ($ arguments [0]); if (is_string ($ arguments [0])) $ this-> foo_for_string ($ arguments [0]);}} private function foo_for_int ($ x) {print ("OH AN) INT! ");} private function foo_for_string ($ x) {print (" Oh A String! ");}} $ x = new magic (); $ x-> foo (3); $ x-> foo (" 3 ");?> __Set and __get This is a great way, __ set and __get method can be used to capture variables and methods that do not exist in an object. Example 9: __set and __get Phpclass foo {function __set ($ name, $ val) {print ("Hello, You Tried to Put $ Val in $ Name");} Function __Get ($ name) {print (" Hey you asked for $ name ");}} $ x = new foo (); $ x-> bar = 3; print ($ x-> winky_winky);?> Type indication in PHP5, you can be in object It is specified that its parameters must be an example of another object.
Example 10: Type indication Phpclass foo {// code ...} Class bar {public function process_a_foo (foo $ foo) {// Some code}} $ b = new bar (); $ f = new foo () $ b-> process_a_foo ($ f);?> It can be seen that we can explicitly indicate the name of an object before the parameter, PHP5 will recognize that this parameter will be an object instance. Static member static members and static methods are referred to as "Class Methods" and "Class Variables" in the term "Class Methods" and "Class Variables". The Object Method is allowed to be called before an object is not instantiated. Similarly, the "object variable" can be independently operated before an object is not instantiated (not required to control the method of an object). Example 11: Objects and object variables PhpClass Calculator {Static Public $ Pi = 3.14151692; Static Public Function Add ($ x, $ y) {return $ x $ y;}} $ s = Calculator :: $ PI $ Result = Calculator :: Add (3, 7); Print ("$ result");?> Exception handling exception handling is an ideal method for handler errors, there is this concept in Java and C , we are happy It sees that this application has been added in PHP5. You can try to use "Try" and "catch" to control the error. Example 12: Exception Handling Phpclass foo {Function Divide ($ y) {if ($ y == 0) Throw new Exception ("Cannot Divide By Zero"); Return $ x / $ y;}} $ x = new foo (); try {$ x-> divide (3,0);} catch (Exception $ E) {Echo $ E-> getMessage (); echo "n
n"; ///// Some Catastrophic Measure Here}?> In the above example, we used "try" to perform the statement in the curly bracket. When there is an error, the code will hand over the error to "catch" clauses, "catch" In the clause, you need to specify whether to hand over an object to an object, so that the code structure looks clearer, because now we can handle all the error messages to an object. Custom Error Handling You can easily control an accident in your program with customized code. You just need to have a self-ended control class from the exception class. In your own error control class, you need to have a constructor and a getMessage method, the following is an example.