Preliminary PHP5

zhaozj2021-02-16  43

Although PHP5 is not officially released (the development version has been downloaded), but we can start to experience a 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 handling (Exception) * Name Space (Namespace)

Before you start, you have to declare two points:

* Examples in the article In order to explain how to operate, some parts use PHP4 performance means, which is just to improve the readability of the article. * The part of the part described in the article and the final release of php5 may have some access

Before PHP5 is not finally released, you can download it from http://snaps.php.net to the latest compiling version from http://snaps.php.net to personally experience these new features that PHP5 brings us.

New object mode

The objects in PHP5 have been made than the system, more comprehensive adjustments, and now there may appear to be 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 * Object's clone * Objects private, public and protected mode * interface (interfaces) * Abstract class * __call * __set and __get * static member

Constructor and destructor

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 description function 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 ();?>>

In the above example, when you terminate the foo class, its destructor will be called, and "BYE BYE" will be output in the previous example.

Object reference

As we all know, in PHP4, the transfer variable gives a function or method. It is actually a copy of this variable, which means that you pass to a function or method is a copy of this variable, unless you use the 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: References of objects

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's clone

As mentioned 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 methods exist in many other applications, so you don't have to worry about its stability. :)

Private, public and protection models in the object

In PHP4, all methods and variables of an object are public, which means that you can operate any variables and methods in an external operation of an object. 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 can only be used inside the class $ x2 = new foo2 (); $ x2-> display ();?>>

Tip: The variables in the object always exist in private form. Direct operation of the variable in an object is not a good object-oriented programming habit, better way to hand over the variable you want to the method of object processing .

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.

? Example 5: Interface

This has a great help to improve the readability and popularity of the code. You can see the above example. The object foo contains two interfaces of DisplayAble and Printable. At this time, we can clearly know that the object foo will have A Display () method and a print () method, just understand the interface part, you can easily operate the object without having to worry about how the object is operated.

Abstract class

Abstract classes cannot be instantiated. Abstract classes are the same as other classes, allowing definition variables and methods. Abstract classes can also define an abstract way, and the abstract class method will not be executed, but it is possible to perform in its derived class.

Example 6: Abstract class

x = $ x;}}}}}}}}}}} {// code}}? >

__call

PHP5 object adds a dedicated method __call (), which is used to monitor other methods in an object. If you try to call a method that does not exist in an object, the __ call method will be called automatically.

Example 7: __ call

dostuff (); $ x-> fancy_stuff ();?>

This special method can be used to implement "Overloading" action so you can check your parameters and pass the parameters by calling a private method.

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 way, __ set and __get method 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 indicate an instance of its parameters must be another object in the method's method.

Example 10: Type indication

Process_a_foo ($ f);?>

It can be seen that we can explicitly indicate the name of an object before the parameter, and PHP5 will recognize that this parameter will be an object instance.

Static member

Static members and static methods are referred to as "Class Methods" and "Class Variables" in the term static member and static methods. The Object Method is allowed to be called before an object is not instantiated. Similarly, the "object variable" can be independently operated before an object is not instantiated (not required to control the method of an object).

Example 11: Object Method and Object Variables

Abnormal processing

Abnormal handling is an ideal method for handler errors. In Java and C , we have this concept. We are happy to see that in PHP5 has joined this application. You can try to use "Try" and "catch" to control the error.

Example 12: abnormal treatment

Divide (3, 0);} catch (Exception $ e) {Echo $ E-> getMessage (); echo "n
n"; // some catastrophic measure here}? > In the above example, we used "try" to perform the statement in the curly bracket. When there is an error, the code will hand over the error to the "catch" clause, in the "Catch" clause, you need Indicates that the error is handed over to an object, so that the code structure looks clearer, because now we can handle all the error messages to an object.

Custom error handling

You can easily control your procedures in your program with customized code. You just need to have a self-ended 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 Processing

data = $ data;.} Function getMessage () {return $ this-> data "caused a WEIRD EXCEPTION! ";}}?>

Now we can use "Throw New WeirdProblem" to throw an error handle, if the error occurs in the code block of "Try", PHP5 will automatically handle the error to the "catch" section.

Name space

Namespace is useful for class or function packets for classes. It can combine some associated classes or functions to make it easier to call.

Example 14: Name Space

>

Note How do you need to use namespace, in actual use, you may need to declare two or more names of the same object to do different things, then you can put them in different namespaces. In the middle (but the interface is the same).

Translator Note: This article comes from phpbuilder, we are happy from the above text. We are happy to see some of the newly added excellent features in PHP5. We can also see some Java and C shadows, and now PHP5 is still not officially released, waiting to truly release that day, I hope to bring more PHP enthusiasts more surprises. Friends who are more interested in this area can log in to the official newsgroup of PHP to learn about the update. The newsgroup address is news: //news.php.net, or you can also log in to the web interface http://news.php.net to access. Let us look forward to the release of the new version. :) (Beyond PHP Avenger)

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

New Post(0)