PHP5 2_php tutorial

Source: Internet
Author: User
Tags php website
PHP5 II. Abstract classes cannot be instantiated. Abstract classes, like other classes, allow definition of variables and methods. Abstract classes can also define an abstract method. abstract classes cannot be instantiated. Abstract classes, like other classes, allow definition of variables and methods. An abstract class can also define an abstract method. an abstract class method is not executed, but may be executed in its derived class. Example 6: abstract class X = $ x ;}} class foo2 extends foo {function display () {// Code }}?> The _ call PHP5 object adds a special 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 automatically called. Example 7: __call DoStuff (); $ x-> fancy_stuff ();?> This special method can be used to implement the "overload" action, so that you can check your parameters and pass parameters by calling a private method. Example 8: use _ 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 are great methods. the __set and _ get methods 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 specify in the object method that the parameter must be an instance of another object. Example 10: type indication Process_a_foo ($ f);?> We can see that we can explicitly specify the name of an object before the parameter, PHP5 will identify this parameter as an object instance. Static Member static members and static methods are called "class methods" and "class variables" in terms of face object programming )". An object method can be called before an object is instantiated. Similarly, "object variables" can be independently controlled before an object is instantiated (an object method is not needed ). Example 11: object methods and object variables Exception handling is recognized as an ideal method for handling program errors. this concept exists in Java and C ++, and we are delighted to see that PHP5 has been added to this application. You can try to use "try" and "catch" to control program errors. Example 12: Exception handling Pide (3, 0);} catch (Exception $ e) {echo $ e-> getMessage (); echo "n
N "; // Some catastrophic measure here}?> In the above example, we used "try" to execute the statement in curly brackets. When an error occurs, the code will hand over the error to the "catch" clause for processing, in the "catch" clause, you need to specify that an error should be handed over to an object for processing. this can make the code structure clearer, because now we can give all the error information to an object for processing. Custom error handling you can easily use custom error code to control exceptions in your program. You only need to assign your own error control class from the exception class. in your own error control class, you need a constructor and a getMessage method. The following is an example. Example 13: Custom error handling Data = $ data;} function getMessage () {return $ this-> data. "caused a weird exception! ";}}?> Now we can use "throw new WeirdProblem ($ foo)" to throw an error handle. If an error occurs in the "try" code block, PHP5 automatically submits the error to the "catch" part for processing. The namespace is useful for class grouping or function grouping. It can combine some related classes or functions to facilitate future calls. Example 14: namespace Note the situations in which you need to use namespaces. in actual use, you may need to declare two or more objects with the same name for different purposes, then you can put them in different namespaces (but the interfaces must be the same ). Note: This article is from PHPbuilder. from the above text, we are glad to see some new excellent features in PHP5. We can also see some shadows of Java and C ++. The current PHP5 has not yet been officially released. on that day, we hope to bring more surprises to all PHP enthusiasts. If you are interested in this aspect, you can log on to the official PHP news group to learn about the updates. The newsgroup address is news: // news.php.net, which can also be accessed by logging on to the WEB interface http://news.php.net. Let's look forward to the release of the new version. :) (Beyond PHP Avenger) Note: This article is an original article. the copyright belongs to the author of the article and beyond the PHP website. it is prohibited to reprint any business without the consent of this site. For reprinting non-profit websites and personal websites, please indicate the source. thank you for your cooperation!

The sequence abstract class cannot be instantiated. Abstract classes, like other classes, allow definition of variables and methods. Abstract classes can also define an abstract method. abstract class methods will not be executed...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.