Improvement of PHP5/ZendEngine2

Source: Internet
Author: User
Tags autoload
The object processing part of the new object model PHP has been completely rewritten, providing better performance and more functions. In previous PHP versions, objects are treated as primitive simple types (such as integer and string). the disadvantage of this method is that when a variable is assigned or passed as a parameter, the object is copied. In the new version, objects are referenced by handles. The new object model
The object processing part in PHP has been completely rewritten, providing better performance and more functions. In previous PHP versions, objects were treated as original simple types.
(Such as integer and string). the disadvantage of this method is that when a variable is assigned or passed as a parameter, an object copy is obtained. In the new version,
An object is referenced by a handle, rather than the object value (the handle is considered an object identifier ).
Many PHP programmers may not realize the "copying quirks" of the old object model, so most previous PHP programs do not need to make any changes.
You can run it, or make few changes.

Private and protected members
PHP 5 introduces private and protected member variables that can define visualized class attributes.
Example
Protection member variables can be accessed in the subclass of this class, while private member variables can only be accessed in the class.
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;/* No content is output, which is similar to the following */
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 methods and protection methods are also introduced.
Example:
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 ();
?>
In the previous code, although the keywords "public," "protected" or "private" are not defined in user-defined classes or methods, they can be run without modification.

Abstract classes and methods
PHP 5 also introduces abstract classes and methods. An abstract method only declares the "symbol" of a method and does not provide its implementation. A class containing abstract methods must be declared as "abstract ".
For example:
Phpabstract 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.
Although the "abstract" keyword is not defined in the user-defined class or method in the old code, it can be run without modification.
Interface (Interfaces)
ZEND Engine 2.0 introduces interfaces. A class can implement any interface list.
For example:

Although the keyword "interface" is not defined in the user-defined classes or methods in the old code, it can run normally without modification.

Class Type Hints)
While retaining classes without defining the types, PHP 5 introduces class type prompts to declare them, so as to pass the class of the object to a method through parameters.
For example:
A ($ B); $ a-> B ($ B);?>
These class type prompts are not checked during compilation as some languages that require the type definition, but during runtime. This means:

Is equivalent:

This syntax is only used for objects or classes and does not apply to built-in (built-in) types.

Final keyword (final)
PHP 5 introduces the "final" keyword to define members or methods that cannot be overwritten in child classes.
Example:
Class Foo {final function bar () {//...}?>
Although the "final" keyword is not defined in the user-defined class or method in the code previously written, it can be run without modification.
Object Cloning)
When an object is copied in PHP 4, you cannot determine which copy constructor to run. During replication, PHP 4 depends on the object attributes.
Copies the same copy one by one.
Creating a completely identical copy every time is not always what we want. A good example of replication construction is:
An object that represents a GTK window. it owns all the resources of the window. when you create a copy, you may need one
Window, which has all properties of the original window, but needs to have resources of the new window. In another example, you have
The object references another object. when you copy the parent object, you want to create a new instance of the referenced object so that the replica has a separate copy.
Copying an object is completed by calling the _ clone () method of the object:
$ 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 has been 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
Set necessary attributes in the copy operation. For ease of use, the engine provides a function to import all attributes from the source object, so that it can
To get a copy of a source object with a value, you only need to overwrite the attributes that need to be changed.
Example:
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 construction method
The ZEND Engine allows developers to define the constructor of classes. Classes with constructor methods will first call constructor to construct
This method is applicable to initialization before the class is officially used.
In PHP4, the constructor name is the same as the class name. Because it is common to call a parent class in a derived class
In PHP4, when the class is moved in a large class inheritance, the processing method is a bit clumsy. When a derived class is moved to a different
In the parent class, the name of the constructor of the parent class must be different. in this case, the statements in the derived class about calling the constructor of the parent class must be rewritten.
PHP5 introduces a standard way to define constructor, which is defined by calling their _ construct.
Example:
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, when the PHP5 class cannot find the _ construct () method, it will use the old method, that is, the class name.
To find the constructor. This means that the only possible cause of compatibility problems is that it has been used in previous code.
A method name named _ construct.

Analysis Method
It is very useful to define the destructor. The Destructor can record debugging information, close database connections, and perform other scanning operations.
Work. PHP4 does not have this mechanism, although PHP already supports registering the function to be run at the end of the request.
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:
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 destructor of the parent class. to call this method, you need
The parent :__ destruct () statement is used to call the class destructor.

Constant
PHP 5 introduces the definition of a class constant (per-class constants:
Class Foo {
Const constant = "constant ";
}

Echo "Foo: constant =". Foo: constant. "\ n ";
?>
PHP5 allows constants to contain expressions, but the expressions in the compilation time are calculated,
Therefore, a constant cannot change its value during running.
Class Bar {
Const a = 1 <0;
Const B = 1 <1;
Const c = a | B;
}
?>
Although the "const" keyword is not defined in user-defined classes or methods in previous code,
But it can be run without modification.

Exceptions)
PHP4 does not handle exceptions. PHP5 introduces exception handling models similar to other languages.
Class MyExceptionFoo extends Exception {
Function _ construct ($ exception ){
Parent: :__ construct ($ exception );
}
}

Try {
Throw new MyExceptionFoo ("Hello ");
} Catch (MyExceptionFoo $ 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 do not need to be modified.
To run.

Function return object value
In PHP4, the function cannot return the object value and call the method of the returned object. with Zend Engine 2
(ZEND Engine 2), the following calls are possible:
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 static classes can be initialized.
For example:
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.
For example:

Class Foo {
Public static function aStaticMethod (){
//...
}
}

Foo: aStaticMethod ();
?>

The virtual variable $ this is invalid in the method defined as static.


Instanceof

PHP5 introduces the "instanceof" keyword to determine whether an object is an instance of an object, the derivation of an object, or an interface.

Example:

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 parameters passed by reference in the function can have default values.
For example:

Function my_function (& $ var = null ){
If ($ var = null ){
Die ("$ var needs to have a value ");
}
}
?>


_ Autoload ()


When initializing an undefined class, the __autoload () interceptor function will be automatically called
. The class name is passed to it as the unique parameter of the _ autoload () interception function.
For example:
Function _ autoload ($ className ){
Include_once $ className. ". php ";
}

$ Object = new ClassName;
?>


Method and attribute call overload
All method calls and attribute access can be reloaded using the _ call (), _ get (), and _ set () methods.

Example: _ get () and _ set ()
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 ()

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.