A preliminary study of PHP5 _php tutorial

Source: Internet
Author: User
Although PHP5 has not yet been officially released (the development version already provides the download), we can now begin to experience the surprises that the new version will bring to us. In the following introduction, we will focus on the three features of PHP5. These three major features are: * New Object Mode * exception handling (Exceptions) * namespace (Namespace) before you begin, declare two points: * Examples in the article in order to illustrate how to operate, some parts of the use of the PHP4 means of expression , this is just to improve the readability of the article. * Some of the parts described in the article and the final release of PHP5 may have some discrepancies before the final official release of PHP5, you can always download from http://snaps.php.net to the latest compiled version to personally experience the new features PHP5 brings us. The objects in the new object schema PHP5 have been systematically and comprehensively adjusted, and may look somewhat like Java now. This section focuses on the new object patterns in PHP5, with some simpler examples to illustrate. Let this section be a new starting point for your PHP5 journey. :) * constructor and destructor * Object reference * Clone of Object * Private, public and protected mode * Interface (Interfaces) * Abstract class * __call * __set and __get * Static member constructors and destructors in PHP4 , this function becomes the constructor for the object when the function has the same name as the object, and there is no concept of destructors in PHP4. In PHP5, the constructors are uniformly named __construct, and the concept of destructors is introduced, and is uniformly named __destruct. Example one: constructors and destructors x = $x; } function display () {print ($this->x),} function __destruct () {print ("Bye Bye");}} $o 1 = new Foo (4); $o 1->display ();?> in the example above, when you terminate the call to the Foo class, its destructor will be called, and the above example will output "Bye Bye". Object references well, in PHP4, passing a variable to a function or method actually makes a copy of the variable, which 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 in the form of references, and the assignment operation in an object is also a reference operation. Example two: reference to an object x = $x; } function GetX () {return $this->x;}} $o 1 = new Foo; $o 1->setx (4); $o 2 = $o 1; $o 1->setx (5); if ($o 1->getx () = = $o 2->getx ()) print ("Oh my god!"); Cloning the?> object as described above, when an object is always invoked as a reference, what if I want to get a copy of the object? PHP5 provides a new function, that is, the cloning of objects, the syntax is __clone. Example three: Cloning of an object x = $x; } function GetX () {return $this->x;}} $o 1 = new Foo; $o 1->setx (4); $o 2 = $o 1->__clone (); $o 1->setx (5); if ($o 1->getx ()! = $o 2->getx ()) print ("Copies is Independant"), the?> object cloning method exists in many other application languages, so you don't have to worry about its stability. :) Private, public, and protected mode in an object PHP4, all methods and variables of an object are public, which means that you can manipulate any of these variables and methods outside of an object. PHP5 introduces three new modes for controlling this access, which are public, protected (Protected), and private (privately). Common mode (public): Allows manipulation control outside the object. Private mode: Only the methods within this object are allowed to manipulate it. Protected Mode (Protected): Allows this object and its parent object to manipulate it. Example four: Private, public, and protected modes in an object Private_foo (); OK because we are at 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->priva Te_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 $x 2 = new Foo2 (); $x 2->display ();?> tip: Variables in objects are always private, and it is not a good object-oriented programming habit to manipulate variables directly in an object, and a better way to do this is to hand over the variables you want to an object. Interface (Interfaces) it is well known that objects in PHP4 support inheritance, and 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 inherit only once, and multiple inheritance is not supported. However, there is a new noun in PHP5: interface, interface is a special object without specific processing code, it just defines the name and parameters of some methods, then the object can easily use the Implement keyword to integrate the required interface, and then add the specific execution code. Example five: interface This is to improve the readability of the code and the popularity of a great help, through the above example can be seen, the object Foo contains the displayable and printable two interfaces, then we can clearly know that the object Foo will have a display () method and a print () method, you can easily manipulate the object without worrying about how the object's interior works, just to understand the interface part. To be Continued ~ ~

http://www.bkjia.com/PHPjc/532172.html www.bkjia.com true http://www.bkjia.com/PHPjc/532172.html techarticle Although PHP5 has not yet been officially released (the development version already provides the download), we can now begin to experience the surprises that the new version will bring to us. In the following introduction, I ...

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