Preliminary PHP5 (2)

zhaozj2021-02-16  57

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 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 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 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 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 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 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 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.

转载请注明原文地址:https://www.9cbs.com/read-21577.html

New Post(0)