Changes in PHP 5ZEND ENGINE 2.0

zhaozj2021-02-17  46

Changes in PHP 5 / ZEND ENGINE 2.0

New Object Model.

PHP's handling of objects has been completely rewritten, allowing for better performance and more features. In previous versions of PHP, objects were handled like primitive types (for instance integers and strings). The drawback of this method was that semantically the whole object was copied .

Many PHP Program of The Copying Quirks of The Old Object Model And, Therefore, The Majority Of PHP Applications Will Work Out of The Box, or with Very Few Modifications.

Private and protected members

PHP 5 Introduces Private and Protected Member Variables, They Allow Do Define The Visibility of Class Properties.

EXAMPLE

Protected Member Variables Can Be Accessed In Classes Extending The Class They Are Declared in, WHEREAS Private Member Variables Can Only Be Accessed by The Class They Belong To.

Hello; Print "Myclass :: Printhello ()". $ This-> bar; print "Myclass :: printhello ()". $ This-> foo;}} MyClass2 extends MyClass {protected $ Foo; function printHello () {MyClass :: printHello (); / * Should print * / print "MyClass2 :: printHello ()" $ this-> Hello;. / * Should not print out anything * / Print "Myclass2 :: Printhello ()". $ This-> bar; / * ShouldN't Print (not Declared) * / print "Myclass2 :: PrintHello ()". $ This-> foo; / * Should Print * /}} $ Obj = new myclass (); print $ obj-> hello; / * shouldn't print out anything * / print $ obj-> bar; / * shopn't print out anything * / print $ obj- > Foo; / * shouldn't print out anything * / $ OBJ-> PrintHello (); / * Should print * / $ obj = new myclass2 (); Print $ O BJ-> Hello; / * ShouldN't print out anything * / print $ obj-> bar; / * shouth't print out anything * / print $ obj-> foo; / * shouth't print out anything * / $ Obj-> printhello ();?> Private and protected methods

WITH PHP 5 (Zend Engine 2), Private and Protected Methods Are Also Introduces.

EXAMPLE

APRIVATEMETHOD ();}} Class Bar Extends foo {public function apublicMethod () {echo "bar :: apublicMethod () Called./n"; $ this-> AprotacectedMethod ();}} $ o = new bar; $ o- > apublicMethod ();?> Old code That Has No User-defined classes or functions name "public," Protected "or" private "Should Run without Modifications.

Abstract Classes and Methods

PHP 5 also introduces abstract classes and methods. An abstract method only declares the method's signature and does not provide an implementation. A class that contains abstract methods needs to be declared abstract.

EXAMPLE

? Test ();?>

Abstract Classes Cannot Be Instantiated. Old Class That Has No User-Defined Classes Or Functions Named 'Abstract' Should Run WITHOUT MODIFICATIONS.

Interfaces

The Zend Engine 2.0 Introduces Interfaces. A class May Implement an Arbitrary List of Interfaces.

EXAMPLE

Old Code That Has No User-Defined Classes or Functions Named 'Interface' Or 'IMPLEMENTS' Should Run WITHOUT Modifications.

Class Type Hints

While Remaining Loosely Typed PHP 5 Introduces The Ability To Use Class Type Hints To Declare The Expected Class of Objects That Are Passed As Parameters To a Method.example

A ($ b); $ A-> b ($ b);?>>>>>>>>>>

THESE CLASS TYPE HINTS Are Not Checked Upon Compilation, AS Would Be The Case In a Typed Language, But During Runtime. This Means That:

IS Equivalent TO:

This Syntax Only Applies To Objects / Classes, Not Built-in Types.

Final

PHP 5 Introduces The "Final" Keyword to Declare Final MEMBERS AND Methods. Methods and Members Declared Final Cannot Be Overridden by Sub-Classes.

EXAMPLE

Old Code That Has No User-Defined Classes Or Functions Named 'Final' Should Run WITHOUT MODIFICATIONS.

Object cloning

PHP 4 (Zend Engine 1.0) offered no way a user could decide what copy constructor to run when an object is duplicated. During duplication, PHP 4 did a bit for bit copy making an identical replica of all the object's properties.

Creating a copy of an object with fully replicated properties is not always the wanted behavior. A good example of the need for copy constructors, is if you have an object which represents a GTK window and the object holds the resource of this GTK window, when you create a duplicate you might want to create a new window with the same properties and have the new object hold the resource of the new window. Another example is if your object holds a reference to another object which it uses and when you replicate the parent Object you want to create a new instance of this other Object So That The Replica Has Its Own Separate Copy.an Object Copy Is Created by Calling The Object'S __clone () Method:

__clone ();?>

When the developer asks to create a new copy of an object, the Zend Engine will check if a __clone () method has been defined or not. If not, it will call a default __clone () which will copy all of the object's properties. If a __clone () method is defined, then it will be responsible to set the necessary properties in the created object. For convenience, the engine will supply a function that imports all of the properties from the source object, so that they can start with A by-value replica of the source object, and only override printness.

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. "/ n"; $ OBJ = $ OBJ -> __ clone (); Print $ OBJ-> ID. "/ n"; Print $ Obj-> name. "/ n"; Print $ Obj-> address. "/ n";> Unified Constructionors

The Zend Engine allows developers to declare constructor methods for classes. Classes which have a constructor method call this method on each newly-created object, so it is suitable for any initialization that the object may need before it is used.

With PHP 4, constructor methods were class methods that had the same name as the class itself. Since it is very common to call parent constructors from derived classes, the way PHP 4 worked made it a bit cumbersome to move classes around in a large class .

PHP 5 Introduces A Standard Way of Decilaning Construction Methods by Calling Them by the name __construct ().

EXAMPLE

? for backwards compatibility, if PHP 5 can not find a __construct () function for a given class, it will search for the old-style constructor function, by the name Of The Class. Effectively, It Means That The Only Case That Would Have Compatibility Issues IS IF The Class Had A Method Named__construct () Which Was Used for Different Semantics.

Destructors

Having the ability to define destructors for objects can be very useful. Destructors can log messages for debugging, close database connections and do other clean-up work. No mechanism for object destructors existed in PHP 4, although PHP had already support for registering functions which SHOULD BE Run on Request Shutdown.

PHP 5 introduces a destructor concept similar to that of other object-oriented languages, 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.

EXAMPLE

name = "MyDestructableClass";} function __destruct () {print "Destroying" $ this-> name "/ n"..; }}} $ obj = new mydestructableclass ();?>>

Like constructors, parent destructors will not be called implicitly by the engine. In order to run a parent destructor, one would have to explicitly call parent :: __ destruct () in the destructor body.Constants

PHP 5 Introduces Per-Class Constants:

>

PHP 5 Allows for Expressions WITHIN CONSTANTS, HOWEVER, Constants Are Evaluated At Compile Time, Therefore No constants can be be declared That Rely on runtime information.

Old Code That Has No User-Defined Classes Or Functions Named 'Const' Will Run WITHOUT MODIFICATIONS.

EXCEPTIONS

PHP 4 Had No Exception Handling. PHP 5 Introduces A Exception Model Similar To That of Other Programming Languages.

EXAMPLE

getMessage ();}?>

Old code That Has No User-Defined Classes Or Functions 'Catch', 'Throw' AND 'Try' Will Run WITHOUT MODIFICATIONS.

Dereferencing Objects Returned from functions

In PHP 4 IT Wasn't Possible to Dereference Objects Returned by Functions and Make Further Method Calls on Those Objects. With the advent of zend Engine 2, The Following is now Possible:

DRAW (); ShapeFactoryMethod ("Square" -> DRAW ();?> Static Member Variables of Static Classes Can Now BE Initialized

EXAMPLE

Static Methods

PHP 5 Introduces The 'Static' Keyword To Declare A Method Static, Thus Callable from Outside The Object Context.

EXAMPLE

>

The Pseudo Variable $ this Is Not Available Inside a Method That Has Been Decland.

INSTANCEOF

PHP 5 Introduces The InstanceOf Keyword, That Allows You To Ascertain WHether OR NOT An Object IS An Instance of a Class, or Extends a class, or imports an interface.

EXAMPLE

Static Function Variables

Statics are now treated at compile-time which allows developers to assign variables to statics by reference. This change also greatly improves their performance but means that indirect references to statics will not work anymore.

Parameters That Are Passed by Reference To a Function May Now Have Default Values

EXAMPLE

__ autoload ()

The __autoload () interceptor function will be automatically called when an undeclared class is to be instantiated. The name of that class will be passed to the __autoload () interceptor function as its only argument.

EXAMPLE

>

Overloadable Method Calls and Property Accesses

Both Method Calls and Property Accesses Can Be overloaded Via a__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 ()

x;}} $ foo = new caller (); $ a = $ foo-> test (1, "2", 3.4, true); var_dump ($ a);?> ---------- ----------------

PHP5 B1 has been released, did not hurry to summer!

My site: http://www.vava.cn

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

New Post(0)