Preliminary study on PHP5 two _php tutorial

Source: Internet
Author: User
Abstract class Abstract classes cannot be instantiated. Abstract classes, like other classes, allow you to define variables and methods. Abstract classes can also define an abstract method, and the methods of the abstract class will not be executed, although they may be executed in their derived classes. Example VI: abstract class x = $x; }} class Foo2 extends Foo {function display () {//Code}}}?> __call PHP5 has a new dedicated method __call (), which is used to monitor other methods in an object. If you try to invoke a method that does not exist in an object, the __call method will be called automatically. Example VII: __call Dostuff (); $x->fancy_stuff ();?> This particular method can be used to implement an "overload (overloading)" Action so that you can examine your parameters and pass the arguments by calling a private method. Example VIII: Using __call to achieve "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 method, and the __set and __get methods can be used to capture variables and methods that do not exist in an object. Example IX: __set and __get bar = 3; Print ($x->winky_winky);?> type indicates that in PHP5, you can indicate in an object's method that its argument must be an instance of another object. Example Ten: Type indication Process_a_foo ($f);?> can see that we can explicitly specify the name of an object before the argument, and PHP5 will recognize that this parameter will be an object instance. Static member static members and static methods are referred to as "Object method (class methods)" and "Object variable (class variables)" In the terminology of polygon object programming. The object method is allowed to be called before an object is instantiated. Similarly, an "object variable" can be controlled independently of an object before it is instantiated (it does not need to be controlled by an object's method). Example 11: Object methods and Object variables Exception handling exception handling is a well-known method of handling program errors, and in both Java and C + +, we are delighted to see that the PHP5 has been added to this application. You can try using "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}?> The above example, we use "Try" to execute the statement in curly braces, when an error occurs, the code will give the error to the "catch" clause to handle, in the "catch" clause, you need to indicate the Handing the error to an object processing makes the code structure look clearer, because now we can give all the error information to an object to handle. Custom error handling you can easily control the unexpected in your program with custom handling of the wrong code. You just need to derive your own error control class from the exception class, and in your own error control class you need to have a constructor and a GetMessage method, and here's 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, and if the error occurs in a code block of "Try", PHP5 will automatically give the error to the "Catch" section for processing. namespace namespaces are useful for grouping or grouping functions on classes. It can combine some related classes or functions to make it easier to call later. Example 14: namespaces Note that you need to use namespaces in situations where you might want to declare two or more names of objects to do different things, and then you can put them in separate namespaces (but the interface is the same). Translator Note: This article from Phpbuilder, from the above text we are pleased to see the PHP5 in the new additions to some of the outstanding features. We can also see some Java and C + + shadow, now the PHP5 has not officially released, wait until the actual release that day, hoping to bring all the PHP enthusiasts more surprises. Friends who are more interested in this area can log in to the official PHP newsgroup to learn about the update. The newsgroup address is news://news.php.net and can also be accessed by logging in 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 the original article, the copyright of the article author and beyond the PHP site all, without the consent of the site, prohibit any commercial reprint. Non-profit sites and personal sites reproduced please specify the source, thank you for your cooperation!

http://www.bkjia.com/PHPjc/532171.html www.bkjia.com true http://www.bkjia.com/PHPjc/532171.html techarticle abstract class Abstract classes cannot be instantiated. Abstract classes, like other classes, allow you to define variables and methods. Abstract classes can also define an abstract method, the methods of abstract classes will not be ...

  • Related Article

    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.