Preliminary PHP5 (1)

zhaozj2021-02-16  53

Preliminary PHP5 (1)

Author: Luis Argerich translation: Avenger Source: PHPBuilde PHP5 though not yet officially released (development version is available for download), but now we can begin to experience the new version will bring us surprises. In the following introduction, we will focus on the three major features in PHP5. These three features are: * New object mode * Exception * Name Space (Namespace) Before starting, declare two points: * Examples in the article In order to explain how to operate, some parts are used The representation of PHP4, which is just to improve the readability of the article. * The final release of the part described in the article may have some access to the PHP5 without the final formal release, you can freely download to the latest compiling version from http://snaps.php.net to personally experience the PHP5 Bring these new features. The objects in the new object mode PHP5 have been made than the system, more comprehensive adjustments, and now there may be some similar to Java. This section focuses on new object patterns in PHP5, and has given some more simple example to illustrate. Let this festival becomes a new starting point for your PHP5 trip. :) * Constructor and Destructor * Objects * Objects' Clone * Objects Private, Public and Protected Mode * Interface (Interfaces) * Abstract Class * __call * __set and __get * Static member constructor and Structure functions in PHP4, when the function is the same name as the object, this function will become the constructor of the object, and there is no concept of destructor in PHP4. In PHP5, the constructor is named __construct, and the concept of the destructor is introduced, and is named __destruct. Example 1: Constructor and destructor x = $ x;} Function Display () {print ($ this-> x);} Function __destruct () {Print ("Bye Bye");}} $ O1 = New Foo (4); $ O1-> Display ();>> When you terminate the foo class, The destructor will be called, and "BYE BYE" will be output in the above example. The object's reference is well known. In PHP4, the transfer variable gives a function or method. It is actually a copy of this variable, which means that you pass the function or method is a copy of this variable, unless you use a reference Symbol "&" to declare to be a reference, not a copy. In PHP5, the object is always present in the form of a reference, and the assignment operation in the object is also a reference operation.

Example 2: The reference to the object 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! ");> The clone of the object As described above, when an object is called in the form of a reference, if I want to get a copy of the object, what should I do? PHP5 provides a new feature, which is the clone of the object, the syntax is __clone. Example 3: Clone of the Object 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");> Object cloning method exists in many other applications, so you don't have to worry about its stability. :) Among the private, public, and protection mode php4 in the object, all methods and variables of an object are public, which means that you can operate any of the object's external operations. PHP5 introduces three new models for controlling such access rights, which are: public, protected, and private (private). Public mode: Allow operational control outside the object. Private Mode: Only the method within this object is allowed to operate. Protected mode: Allows this object and its parent objects to operate.

Example 4: Private, public and protected mode in the object private_foo (); // ok Because We are in the Same Class We can call private methods print ("I'm protected");} private function private_foo () {$ this-> x = 3; print ("I'm 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 $ x2 = new foo2 (); $ x2-> display ();?> Tip: The variables in the object always exist in private form, direct Operating variables in an object is not a good object-oriented programming habit, a better way to handle the variables you want to a method of object. Interfaces, well known, objects in PHP4 support, to make an object becomes a derived class of another object, you need to use a code similar to "Class Foo Extends Parent" to control. In PHP4 and PHP5, one object can only inherit once, and multiple inherits are not supported. However, in PHP5, a new noun: interface is generated, the interface is a special object without specific processing code. It only defines the name and parameters of some methods. Since this object can be convenient to use the 'Implement' keyword The needed interface is integrated, then add the specific execution code.

转载请注明原文地址:https://www.9cbs.com/read-21578.html

New Post(0)