Improvement of PHP 5Zend Engine 2.0 (2)

zhaozj2021-02-16  46

[Connect on page]

PHP 5 / Zend Engine 2.0 improvements

PHP5 allows constants to include expressions, but the expression in compiling time is calculated,

Therefore, constants cannot change its value in operation.

Class bar {

Const

A = 1 << 0;

Const

B = 1 << 1;

Const

C = a | b;

}

?>

Although the "const" keyword is not defined in the user-defined class or method in the previous code.

But you can run without modification.

Excetions

There is no abnormality in PHP4, and PHP5 introduces an exception handling model similar to other languages.

getMessage ();}?>

Although the user-defined class or method in the previous code is not defined, 'Catch', 'Throw' and 'Try' Keywords, but there is no need to modify

You can run it.

Function Returns the object value

In PHP4, the function is not possible to return the value of the object and the object returned by the 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

E.g:

Static Methods

PHP5 introduces keyword 'static' to define a static method, which can be called from the object.

E.g:

>

Virtual Variable $ This is invalid in the method defined as a static (static).

INSTANCEOF

PHP5 introduces the "instanceof" keyword to determine if an object is an instance of an object, or a derived of an object, or uses an interface.

Example:

static function variables (static function variables)

All static variables are now processed at compile, which allows developers to specify static variables by reference. This change increases efficiency but means that it is impossible to indirectly quote for static variables.

Parameters passed by reference in the function allow for default values

E.g:

__AUTOLOAD ()

When initializing an undefined class, the __ autoload () intercepting function (Interceptor function) will be automatically adjusted

use. The class name will be passed as the __autoload () intercept function. The unique parameter is passed to it.

E.g:

>

Overloading of methods and attribute calls

All method calls and attribute access can be reloaded universal__call (), __get (), and __set () methods.

Example: __get () and __set ()

1, "b" => 2, "c" => 3); function __get ($ nm) {print "getting [$ nm ] / n "; if ($ this-> x [$ nm])) {$ r = $ this-> x [$ nm]; print" returning: $ r / n "; returnid $ 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 ()

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

New Post(0)