PHP 5 was launched yesterday -- New Features of PHP 5/Zend Engine 2.0

Source: Internet
Author: User
Tags php website


Preface

Today, I suddenly thought of a revolution on the official PHP website and saw the announcement of PHP5 at a glance. Although I have seen the predictions of PHP5 before, I have carefully read the new features of PHP 5/Zend Engine 2.0, and a JAVA atmosphere is coming...
This article was first translated on the CSDN website to attract readers.

New Features of PHP 5/Zend Engine 2.0
Xu call Chun translation sfwebsite@hotmail.com
Http://www.php.net/zend-engine-2.php

Brand new object model
The object processing part in PHP has been completely rewritten, providing better performance and more functions. In earlier versions of PHP, objects and built-in variable types (such as integer and string) are processed in the same way. The drawback is that when a variable is assigned as an object or an object is passed as a parameter, the result is an object copy. In the new version, an object is referenced by a handle, rather than its value. (The handle can be recognized as the object identifier)
Many PHP programmers may not be aware of the "replication quirks" of the previous object model, so the previous PHP program will run without any changes or just a small change.
Private and protected members
PHP 5 introduces private and protected member variables that can define when class attributes can be accessed.
Example
Class protection member variables can be accessed in the extension class of this class, while private member variables can only be accessed in this class.
<? Php
Class MyClass {
Private $ Hello = "Hello, World! \ N ";
Protected $ Bar = "Hello, Foo! \ N ";
Protected $ Foo = "Hello, Bar! \ N ";

Function printHello (){
Print "MyClass: printHello ()". $ this-> Hello;
Print "MyClass: printHello ()". $ this-> Bar;
Print "MyClass: printHello ()". $ this-> Foo;
}
}

Class MyClass2 extends MyClass {
Protected $ Foo;

Function printHello (){
MyClass: printHello ();/* shocould print */
Print "MyClass2: printHello ()". $ this-> Hello;/* Shouldn't print out anything */
Print "MyClass2: printHello ()". $ this-> Bar;/* Shouldn't print (not declared )*/
Print "MyClass2: printHello ()". $ this-> Foo;/* shocould print */
}
}

$ Obj = new MyClass ();
Print $ obj-> Hello;/* Shouldn't print out anything */
Print $ obj-> Bar;/* Shouldn't print out anything */
Print $ obj-> Foo;/* Shouldn't print out anything */
$ Obj-> printHello ();/* shocould print */

$ Obj = new MyClass2 ();
Print $ obj-> Hello;/* Shouldn't print out anything */
Print $ obj-> Bar;/* Shouldn't print out anything */
Print $ obj-> Foo;/* Shouldn't print out anything */
$ Obj-> printHello ();
?>
Private and protection methods
In PHP 5 (ZEND Engine 2), private and protection methods are also introduced.
Example:
<? Php
Class Foo {
Private function extends vatemethod (){
Echo "Foo: aPrivateMethod () called. \ n ";
}

Protected function aProtectedMethod (){
Echo "Foo: aProtectedMethod () called. \ n ";
$ This-> aPrivateMethod ();
}
}

Class Bar extends Foo {
Public function aPublicMethod (){
Echo "Bar: aPublicMethod () called. \ n ";
$ This-> aProtectedMethod ();
}
}

$ O = new Bar;
$ O-> aPublicMethod ();
?>
Keywords such as "public," "protected" or "private" are not defined in user-defined classes or methods in the previous code, but they can be run without editing.
Abstract classes and Methods
PHP 5 also introduces abstract classes and methods. Abstract METHODS only declare method definitions and are not for actual operation. Classes that contain abstract methods must be declared as abstract classes.
Example:
<? Php
Abstract class AbstractClass {
Abstract public function test ();
}

Class ImplementedClass extends AbstractClass {
Public function test (){
Echo "ImplementedClass: test () called. \ n ";
}
}

$ O = new ImplementedClass;
$ O-> test ();
?>
Abstract classes cannot be instantiated. In the previous Code, although the "abstract" keyword is not defined in user-defined classes or methods, it can be run without editing.
Interface
ZEND engine 2.0 introduces interfaces. A class can run any interface list.
Example
Example:
<? Php
Interface Throwable {
Public function getMessage ();
}

Class Exception implements Throwable {
Public function getMessage (){
//...
}
?>
In the previous Code, although the "interface" keyword is not defined in user-defined classes or methods, it can be run without editing.
Class Type Definition
While retaining classes without defining types, PHP 5 introduces the class type definition to declare which class to pass to a method through parameters.
Example
Example:
<? Php
Interface Foo {
Function a (Foo $ foo );
}

Interface Bar {
Function B (Bar $ bar );
}

Class FooBar implements Foo, Bar {
Function a (Foo $ foo ){
//...
}

Function B (Bar $ bar ){
//...
}
}

$ A = new FooBar;
$ B = new FooBar;

$ A-> a ($ B );
$ A-> B ($ B );
?>
These class types are defined at runtime instead of checking in compilation in languages that require predefined types. This means:
<? Php
Function foo (ClassName $ object ){
//...
}
?>
It is equivalent:
<? Php
Function foo ($ object ){
If (! ($ Object instanceof ClassName )){
Die ("Argument 1 must be an instance of ClassName ");
}
}
?>
This syntax is only used for objects or classes and does not apply to built-in types.

Final
PHP 5 introduces the "final" keyword to define members or methods that cannot be overwritten in child classes.
Example:
<? Php
Class Foo {
Final function bar (){
//...
}
}
?>
Although the "final" keyword is not defined in the User-Defined classes or methods in the previous code, the code can be run without editing.
Object cloning
When an object is copied in PHP 4, you cannot decide the copy mechanism. During the replication, PHP 4 copies exactly the same copy as the original object.
We don't have to create a completely identical copy every time. A good example of a replication mechanism is that when an object representing a GTK window has all the resources of the window, when you create a copy, you may need a new window, which has all the properties of the original window, but requires resources of the new window. Another example is that you have an object that references another object. When you copy the parent object, you want to create a new instance of the referenced object so that the replica can reference it.
Copying an object is completed by calling the _ clone () method of the object:
<? Php
$ Copy_of_object = $ object->__ clone ();
?>
When a developer requests to create a new copy of an object, the ZEND engine checks whether the _ clone () method is defined. If not defined, it will call a default _ clone () method to copy all attributes of the object. If this method is defined, this method is responsible for setting necessary attributes in the copy. For convenience, the engine provides a function to import all attributes from the source object, so that it can first obtain a copy of the source object with a value, you only need to overwrite the attributes that need to be changed.
Example:
<? Php
Class MyCloneable {
Static $ id = 0;

Function MyCloneable (){
$ This-> id = self: $ id ++;
}

Function _ clone (){
$ This-> name = $ that-> name;
$ This-> address = "New York ";
$ This-> id = self: $ id ++;
}
}

$ Obj = new MyCloneable ();

$ Obj-> name = "Hello ";
$ Obj-> address = "Tel-Aviv ";

Print $ obj-> id. "\ n ";

$ Obj = $ obj->__ clone ();

Print $ obj-> id. "\ n ";
Print $ obj-> name. "\ n ";
Print $ obj-> address. "\ n ";
?>
Unified constructor name
The ZEND engine allows developers to define the constructor of classes. Classes with constructor methods will first call constructor methods when they are created. constructor methods are suitable for initialization before the class is officially used.
In PHP4, the constructor name is the same as the class name. The method of calling the parent class in a derived class is quite common. Therefore, in PHP4, when the class is moved in a large class inheritance, the processing method is clumsy. When a derived class is moved to a different parent class, the name of the constructor of the parent class must be different. In this case, the statements in the derived class that call the constructor of the parent class must be rewritten.
PHP 5 introduces a standard way of declaring constructor methods by calling them by the name _ construct ().
PHP5 introduces method name_construct () to define the constructor.
Example
<? Php
Class BaseClass {
Function _ construct (){
Print "In BaseClass constructor \ n ";
}
}

Class SubClass extends BaseClass {
Function _ construct (){
Parent: :__ construct ();
Print "In SubClass constructor \ n ";
}
}

$ Obj = new BaseClass ();
$ Obj = new SubClass ();
?>
For backward compatibility, PHP5 searches for Constructor through the old method, that is, class name, when the _ construct () method cannot be found in the class. This means that the only possible cause of compatibility problems is that a method name named _ construct () has been used in the previous code.
Analysis Method
It is very useful to define the destructor. The Destructor can record debugging information, close database connections, and perform other operations. PHP4 does not have this mechanism, although PHP already supports registering the function to be run at the end of the request.
PHP 5 introduces a destructor concept similar to that of other object-oriented ages, such as Java: When the last reference to an object is destroyed the object's destructor, which is a class method name % __destruct () % that recieves no parameters, is called before the object is freed from memory.
PHP5 introduces a structure method similar to other object-oriented languages such as Java: when the reference of the last object is cleared, the system will call the Destructor named _ destruct () before the object is released from the memory.
Example:
<? Php
Class MyDestructableClass {
Function _ construct (){
Print "In constructor \ n ";
$ This-> name = "MyDestructableClass ";
}

Function _ destruct (){
Print "Destroying". $ this-> name. "\ n ";
}
}

$ Obj = new MyDestructableClass ();
?>
Similar to the constructor method, the engine does not call the parent class's destructor. To call this method, you need to use parent ::__ destruct () in the subclass's destructor () statement.
Constant
PHP 5 introduces the definition of Class constants:
<? Php
Class Foo {
Const constant = "constant ";
}

Echo "Foo: constant =". Foo: constant. "\ n ";
?>

PHP5 allows constants to have expressions, but the expressions in the compilation constant will be calculated. Therefore, constants cannot change their values at runtime.
<? Php
Class Bar {
Const a = 1 <0;
Const B = 1 <1;
Const c = a | B;
}
?>
In the previous Code, although the "const" keyword is not defined in user-defined classes or methods, it can be run without editing.
Exceptions
PHP 4 had no exception handling. PHP 5 introduces a exception model similar to that of other programming ages.
There is no exception handling in PHP4, and PHP5 references an exception processing model similar to other languages.
Example:
<? Php
Class MyExceptionFoo extends Exception {
Function _ construct ($ exception ){
Parent: :__ construct ($ exception );
}
}

Try {
Throw new MyExceptionFoo ("Hello ");
} Catch (MyException $ exception ){
Print $ exception-> getMessage ();
}
?>
Although the 'catch ', 'throw', and 'try' keywords are not defined in user-defined classes or methods in the previous code, they can be run without editing.
Function return object Value
In PHP 4 it wasn' t possible to dereference objects returned by functions and make further method CILS on those objects. With the advent of Zend Engine 2, the following is now possible:
In PHP4, it is impossible for a function to return the value of an object and call the method of the returned object. In ZEND Engine 2, all this is possible:
<? Php
Class Circle {
Function draw (){
Print "Circle \ n ";
}
}

Class Square {
Function draw (){
Print "Square \ n ";
}
}

Function ShapeFactoryMethod ($ shape ){
Switch ($ shape ){
Case "Circle ":
Return new Circle ();
Case "Square ":
Return new Square ();
}
}

ShapeFactoryMethod ("Circle")-> draw ();
ShapeFactoryMethod ("Square")-> draw ();
?>
Static member variables in the static class can now be initialized
Example
<? Php
Class foo {
Static $ my_static = 5;
}

Print foo: $ my_static;
?>
Static Method
PHP5 introduces the keyword 'static 'to define a static method, which can be called outside the object.
Example:
<? Php
Class Foo {
Public static function aStaticMethod (){
//...
}
}

Foo: aStaticMethod ();
?>
The virtual variable $ this is invalid in the static method.
Instanceof
PHP5 introduces the keyword instanceof to determine whether an object is an instance of an object, the derivation of an object, or an interface.
Example:
<? Php
Class baseClass {}

$ A = new baseClass;

If ($ a instanceof basicClass ){
Echo "Hello World ";
}
?>
Static function variables
All static variables are now processed at compilation, which allows developers to specify static variables by referencing them. This change increases the efficiency but does not mean indirect reference to static variables.
The default value can be defined by parameters of the address transfer mode in the function.
Example:
<? Php
Function my_function (& $ var = null ){
If ($ var = null ){
Die ("$ var needs to have a value ");
}
}
?>
_ Autoload ()
When initializing an undefined class, the engine will automatically call the _ autoload () interceptor function. This class will be passed to it as the unique parameter of the _ autoload () interceptor function.
Example:
<? Php
Function _ autoload ($ className ){
Include_once $ className. ". php ";
}

$ Object = new ClassName;
?>
Method and attribute call overload
The common _ call (), _ get (), and _ set () methods can be overloaded for method and attribute calls.

Example: _ get () and _ set ()
<? Php
Class Setter {
Public $ n;
Public $ x = array ("a" => 1, "B" => 2, "c" => 3 );

Function _ get ($ nm ){
Print "Getting [$ nm] \ n ";

If (isset ($ this-> x [$ nm]) {
$ R = $ this-> x [$ nm];
Print "Returning: $ r \ n ";
Return $ r;
} Else {
Print "Nothing! \ N ";
}
}

Function _ set ($ nm, $ val ){
Print "Setting [$ nm] to $ val \ n ";

If (isset ($ this-> x [$ nm]) {
$ This-> x [$ nm] = $ val;
Print "OK! \ N ";
} Else {
Print "Not OK! \ N ";
}
}
}

$ Foo = new Setter ();
$ Foo-> n = 1;
$ Foo-> a = 100;
$ Foo-> a ++;
$ Foo-> z ++;
Var_dump ($ foo );
?>
Example: _ call ()
<? Php
Class Caller {
Var $ x = array (1, 2, 3 );

Function _ call ($ m, $ ){
Print "Method $ m called: \ n ";
Var_dump ($ );
Return $ this-> x;
}
}

$ Foo = new Caller ();
$ A = $ foo-& gt; test (1, "2", 3.4, true );
Var_dump ($ );
?>

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.