PHP5 was launched yesterday -- new features of PHP5/ZendEngine2.0

Source: Internet
Author: User
Tags constant definition
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 carefully read the new features of PHP5/ZendEngine2.0, and I came across a JAVA atmosphere... this article was first translated on the CSDN website to attract readers. PHP5/ZSyntaxHighlight

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. PHP 5/Zend Engine 2.0 new features Xu call spring translation sfwebsite@hotmail.com http://www.php.net/zend-engine-2.php brand new object model PHP in the object processing part has been fully rewritten, with better performance and more features. 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 an object identifier.) many PHP programmers may not be aware of the "copy quirks" of the previous object model, so the previous PHP program does not need to make any changes, you can run the private and protected member PHP 5 with only a few changes to introduce private and protected member variables, which can define when class attributes can be accessed. Protection member variables of the instance class can be accessed in the extension class of the class, while private member variables can only be accessed in this class. 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;/* Shouldnt print out anything */print "MyClass2: printHello ()". $ this-> Bar;/* Shouldnt print (not declared) */print "MyClass2: print Hello ()". $ this-> Foo;/* shocould print */} $ obj = new MyClass (); print $ obj-> Hello; /* Shouldnt print out anything */print $ obj-> Bar;/* Shouldnt print out anything */print $ obj-> Foo; /* Shouldnt print out anything */$ obj-> printHello ();/* shocould print */$ obj = new MyClass2 (); print $ obj-> Hello; /* Shouldnt print out anything */print $ obj-> Bar;/* Shouldnt print out anything */print $ obj -> Foo;/* Shouldnt print out anything */$ obj-> printHello ();?> Private and protection methods are also introduced in PHP 5 (ZEND Engine 2. Example: APrivateMethod () ;}} class Bar extends Foo {public function aPublicMethod () {echo "Bar: aPublicMethod () called. "; $ 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: 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 the interface. A class can run any interface list. Example: 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 the class without defining the type, PHP 5 introduces the class type definition to declare which class to pass to a method through parameters. Example: 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: It is equivalent: 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 the members or methods that cannot be overwritten in the subclass. Example: 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 PHP 4: When an object is copied, the user cannot decide the mechanism of copying. 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: _ 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: 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. ""; $ obj = $ obj->__ clone (); print $ obj-> id. ""; print $ obj-> name. ""; print $ obj-> address. "";?> The unified constructor name 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 the method name__ construct () to define the constructor. Example 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. 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 objects 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: Name = "MyDestructableClass";} function _ destruct () {print "Destroying". $ this-> name. "" ;}}$ 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 class constant definition: PHP5 allows constants to have expressions, but the expressions in the compilation constant will be calculated. Therefore, constants cannot change their values at runtime.

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.