PHP5 _ PHP Tutorial

Source: Internet
Author: User
PHP5 1. Although PHP5 has not yet been officially released (the development version has been provided for download), we can now start to experience the surprises that the new version will bring us. In the following introduction, although PHP5 has not been officially released (the development version has been provided for download), we can start to experience the surprises of the new version. In the following introduction, we will focus on three major features in PHP5. These three features are as follows: * New Object Mode * Exception handling * Namespace two points must be declared before the start: * The example in this article is to illustrate how to perform the operation. some parts use the PHP4 expression method, which is only to improve the readability of the article. * Some of the parts described in this article may differ from the final release version of PHP5 before PHP5 is officially released, you can download the latest build from the http://snaps.php.net at any time to experience the new features PHP5 brings to us. The objects in the new object mode PHP5 have been systematically and comprehensively adjusted. the current situation may look similar to Java. This section describes the new object mode in PHP5 and provides some simple examples. Let this section be a new starting point for your PHP5 journey. :) * Reference of * constructor and Destructor * object * clone of object * private, public, and protected mode * interface (Interfaces) of the object) * abstract classes * _ call * _ set and _ get * static member constructors and Destructor are in PHP4. when a function has the same name as an object, this function will become the constructor of this object, and there is no destructor concept in PHP4. In PHP5, constructor is uniformly named as _ construct, and the concept of destructor is introduced. it is uniformly named as _ destruct. Example 1: constructor and Destructor X = $ x;} function display () {print ($ this-> x);} function _ destruct () {print ("bye ");}} $ o1 = new foo (4); $ o1-> display ();?> In the above example, when you terminate the call to the foo class, the destructor will be called, and "bye" will be output in the above example ". Object references are well known. in PHP4, when a variable is passed to a function or method, the variable is actually copied once, this means that you pass a Copy of the variable to the function or method, unless you use the reference symbol "&" to declare that you want to make a reference, not a Copy. In PHP5, objects always exist as references, and assignment operations in objects are also a reference operation. Example 2: Object reference X = $ x;} function getX () {return $ this-> x ;}}$ o1 = new foo; $ o1-> setX (4 ); $ o2 = $ o1; $ o1-> setX (5); if ($ o1-> getX () = $ o2-> getX ()) print ("Oh my god! ");?> Object cloning as described above, what should I do if I want to get a copy of an object when an object is always called as a reference? PHP5 provides a new function, namely object cloning, with the syntax of _ clone. Example 3: Object cloning X = $ x;} function getX () {return $ this-> x ;}}$ o1 = new foo; $ o1-> setX (4 ); $ o2 = $ o1->__ clone (); $ o1-> setX (5); if ($ o1-> getX ()! = $ O2-> getX () print ("Copies are independant");?> The object cloning method exists in many other application languages, so you don't have to worry about its stability. :) In the private, public, and protection mode PHP4 of an object, all methods and variables of an object are public, this means that you can operate any of the variables and methods in an object's external operations. PHP5 introduces three new access control modes: Public, Protected, and Private ). Public mode: allows operation control outside the object. Private: only the methods in the object can be controlled. Protected Mode (Protected): allows this object and its parent object to control its operations. Example 4: private, public, and protected modes of objects Private_foo (); // OK because we are in the same class we can call private methods print ("Im protected");} private function private_foo () {$ this-> x = 3; print ("Im private") ;}} class foo2 extends foo {public function display () {$ this-> protected_foo (); $ this-> public_foo (); // $ this-> private_foo (); // Invalid! The function is private in the base class }}$ x = new foo (); $ x-> public_foo (); // $ x-> protected_foo (); // Invalid cannot call protected methods outside the class and derived classes // $ x-> private_foo (); // Invalid private methods can only be used inside the class $ x2 = new foo2 (); $ x2-> display ();?> Tip: variables in an object always exist in private form. directly operating on variables in an object is not a good object-oriented programming habit, A better way is to hand over the variable you want to an object. Interface (Interfaces) as we all know, objects in PHP4 support inheritance. to make an object a derived class of another object, you need to use code similar to "class foo extends parent" to control it. In PHP4 and PHP5, an object can only be inherited once. Multi-inheritance is not supported. However, a new term is generated in PHP5: interface, which is a special object without specific code processing. it only defines the names and parameters of some methods, the subsequent objects can easily use the implement keyword to integrate the required interfaces, and then add the specific execution code. Example 5: Interface This is of great help to improve the readability and popularity of the code. from the example above, we can see that the object foo contains the displayable and printable interfaces, so we can clearly understand that, the object foo must have a display () method and a print () method. you only need to understand the Interface section, you can easily operate the object without having to worry about how the object operates internally. To be continued ~~~

PHP5 has not yet been officially released (the development version has been provided for download), but now we can start to experience the surprises that the new version will bring us. In the following introduction, I...

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.