Home > Backend Development > PHP Tutorial > A first look at PHP5 2_PHP Tutorial

A first look at PHP5 2_PHP Tutorial

WBOY
Release: 2016-07-13 17:24:12
Original
800 people have browsed it

Abstract classes Abstract classes cannot be instantiated. Abstract classes, like other classes, allow variables and methods to be defined. An abstract class can also define an abstract method. The method of the abstract class will not be executed, but it may be executed in its derived class. Example 6: Abstract class x = $x; } } class foo2 extends foo { function display() { // Code } } ?> __call PHP5 objects have a new special method __call(). This method uses to monitor other methods in an object. If you try to call a method that does not exist on the object, the __call method will be called automatically. Example 7: __call doStuff(); $x->fancy_stuff(); ?> This special method can be used to implement "overloading" actions, so that you can check your parameters and call A private method to pass parameters. 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 This is a great method. 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's method that its parameter must be an instance of another object. Example 10: Type indication process_a_foo($f); ?> It can be seen that we can explicitly specify the name of an object before the parameter, and PHP5 will recognize that this parameter will be an object instance. Static members Static members and static methods are called "object methods (class methods)" and "object variables (class variables)" in object-oriented programming terminology. "Object methods" are allowed to be called before an object is instantiated. Similarly, "object variables" can be controlled independently before an object is instantiated (without using an object's methods to control it). Example 11: Object methods and object variables Exception handling Exception handling is recognized as an ideal method for handling program errors. This concept is available in Java and C++. We are pleased to see that this aspect has been added to PHP5 application. You can try using "try" and "catch" to control program errors. 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 use "try" to execute the statement in the 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 you want to hand over the error to an object. This can make the code structure look clearer, because now we can hand over all error information to an object for handling. Defining Error Handling You can easily use custom error handling code to control accidents in your program. You only need to derive your own error 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. 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 handler. If the error occurs in the "try" code block, PHP5 will automatically hand the error to the "catch" section To handle. Namespace Namespace is useful for grouping classes or functions. It can group some related classes or functions together for later calls. Example 14: Namespace Pay attention to what you need. In actual use, you may need to declare two or more objects with the same name to do different things, then you can put them in different namespaces (but the interface is (to be the same). Translator's Note: This article comes from PHPbuilder. From the above text, we are happy to see some excellent new features added in PHP5. We can also see some shadows of Java and C++. The current PHP5 is still there. It has not been officially released. I hope it will bring more surprises to all PHP enthusiasts when it is actually released. Friends who are more interested in this aspect can log in to the PHP official news group to learn about the updates. The news group address is news: //news.php.net, you can also log in to the WEB interface http://news.php.net to visit. 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 the Beyond PHP website. Any commercial reprinting is prohibited without the consent of this site. Please indicate the source for reprinting on non-profit websites and personal websites. Thank you for your cooperation!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/532171.htmlTechArticleAbstract class Abstract class cannot be instantiated. Abstract classes, like other classes, allow variables and methods to be defined. An abstract class can also define an abstract method. The method of the abstract class will not be executed...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template