PHP 5 yesterday unveiled the--php 5/zend Engine 2.0 new features _php Foundation

Source: Internet
Author: User
Tags constant definition exception handling getmessage throwable zend

Objective

Today, I suddenly thought of the PHP official website on a turn, at one glance saw PHP5 launched the notice. Although I have seen the PHP5 of the previous notice, but still carefully read the PHP 5/zend Engine 2.0 new features of a text, a Java breath on the faces ...
Special will try to translate the text, starting in the CSDN website, readers.

PHP 5/zend Engine 2.0 new features
Xu Xianchun Translation sfwebsite@hotmail.com
http://www.php.net/zend-engine-2.php

A new object model
The object processing part of PHP has been completely rewritten, with better performance and more functionality. In previous versions of PHP, objects were treated in the same way as built-in variable types, such as Integer and string, with the disadvantage that when a variable is assigned to an object or object as a parameter, the object is copied. In a new version, an object is referenced through a handle, not through its value. (a handle can be recognized as an identifier for an object)
Many PHP programmers may not be aware of the "replication quirks" of the previous object model, so the previous PHP program will not need to make any changes, or make small changes to run
Private and protected members
PHP 5 introduces private and protected member variables that can define when a class attribute can be accessed.
Cases
A protected member variable of a class can be accessed in an extended class of that class, whereas a private member variable 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::p Rinthello ()". $this->hello;
Print "MyClass::p Rinthello ()". $this->bar;
Print "MyClass::p Rinthello ()". $this->foo;
}
}

Class MyClass2 extends MyClass {
protected $Foo;

function Printhello () {
MyClass::p Rinthello (); /* Should Print * *
Print "MyClass2::p Rinthello ()". $this->hello; /* shouldn ' t print out anything * *
Print "MyClass2::p Rinthello ()".   $this->bar; /* shouldn ' t print (not declared) * *
Print "MyClass2::p Rinthello ()".   $this->foo; /* Should 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 (); /* Should 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.
Cases:
<?php
Class Foo {
Private Function Aprivatemethod () {
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 previous code, but are run without editing.
Abstract classes and methods
PHP 5 also introduces abstract classes and methods. An abstract method declares only the method definition and is not intended for actual operation. Classes that contain abstract methods need to be declared as abstract classes.
Cases:
<?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 ();
?>
An abstract class cannot be instantiated. The "abstract" keyword is not defined in the user-defined class or method in the previous code, but is run without editing.
Interface
Zend Engine 2.0 introduces the interface. A class can run any list of interfaces.
Example
Cases:
<?php
Interface Throwable {
Public function getMessage ();
}

Class Exception implements Throwable {
Public Function GetMessage () {
// ...
}
?>
The "interface" keyword is not defined in the user-defined class or method in the previous code, but is run without editing.
Class type definition
As the retention class does not need to define a type, PHP 5 introduces a class type definition to declare which class you want to pass to a method by argument.
Example
Cases:
<?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 when they are not checked in compilation, as some languages require a predefined type, but at run time. This means:
<?php
function foo (ClassName $object) {
// ...
}
?>
Equivalent to:
<?php
function foo ($object) {
if (!) ( $object instanceof ClassName)) {
Die ("Argument 1 must be a instance of ClassName");
}
}
?>
This syntax is used only for objects or classes, not for builtin types.

Final
PHP 5 Introduces the "final" keyword to define a member or method that cannot be overridden in a subclass.
Cases:
<?php
Class Foo {
Final function bar () {
// ...
}
}
?>
The "final" keyword is not defined in the user-defined class or method in the previous code, but is run without editing.
Object cloning
PHP 4 When an object is replicated, the user cannot determine the mechanism of the copy. At the time of replication, PHP 4 copies a copy of exactly the same object as the original.
We don't have to build exactly the same copy every time. An example of a good need for a replication mechanism is that when there is an object that represents a GTK window, it owns all the resources of the window, and when you create a copy, you may need a new window that has all the properties of the original window, but it needs a resource for the new window. Another example is that you have an object that references another object, and when you copy the parent object, you want to create a new instance of the referencing object so that the replica references it.
The copy of an object is accomplished by calling the object's __clone () method:
<?php
$copy _of_object = $object->__clone ();
?>
When a developer requests a new copy of an object, the Zend engine checks to see if the __clone () method is defined. If not defined, it invokes a default __clone () method to copy all the properties of the object. If the method is defined, the method is responsible for setting the necessary properties in the copy. For convenience, the engine provides a function to import all the properties from the source object, so that it can get a copy of the source object with the value first, overwriting the property that needs to be changed.
Cases:
<?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";
?>
The Unified constructor method name
The Zend Engine allows developers to define how classes are constructed. A class with a constructor method calls the constructor method first when it is new, and the constructor applies to initialization before the class is formally used.
In PHP4, the constructor method has the same name as the class name. Because it is common to invoke the parent class in a derived class, it is somewhat awkward to do so in PHP4 when a class moves in a large class inheritance. When a derived class is moved to a different parent class, the constructor method name of the parent class must be different, so that the statement in the derived class about calling the parent class constructor needs to be overwritten.
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 construction method.
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 when the class cannot find the __construct () method, the old method, the class name, is used to find the construction method. This means that the only possible compatibility problem is that a method name named __construct () has been used in the previous code.
destructor method
It is very useful to define a destructor method. The destructor can record debugging information, close the database connection, and do other cleanup work. There is no such mechanism in PHP4, although PHP has support for registering functions that need to be run at the end of a request.
PHP 5 introduces a destructor concept similar to which other object-oriented languages, such as Java:when the last Refe Rence to an object is destroyed the object ' destructor, which are a class method name%__destruct ()% that Recieves no para Meters, is called before the object is freed from memory.
PHP5 introduces a destructor similar to other object-oriented languages, such as the Java language: When the last reference to this object is cleared, the system will invoke the destructor named __destruct () before the object is freed from memory.
Cases:
<?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 construction method, the engine will not invoke the destructor of the parent class, and in order to invoke the method, you need to invoke the parent::__destruct () statement in the destructor of the subclass.
Constant
PHP 5 introduces a class constant definition:
<?php
Class Foo {
CONST CONSTANT = "constant";
}

echo "foo::constant =". Foo::constant. "\ n";
?>

PHP5 allows expressions in constants, but expressions in compile-time literals are computed., so constants cannot change their values in the run.
<?php
Class Bar {
Const A = 1<<0;
Const B = 1<<1;
Const C = A | b
}
?>
The "const" keyword is not defined in the user-defined class or method in the previous code, but is run without editing.
Exception
PHP 4 had no exception handling. PHP 5 introduces a exception model similar to that of the other programming languages.
There is no exception in PHP4, PHP5 references an exception-handling model similar to other languages.
Cases:
<?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 the user-defined class or method in the previous code, they can be run without editing.
function returns the value of an object
In the PHP 4 it wasn ' t possible to dereference objects returned from functions and make further method calls on those objects. With the advent of Zend Engine 2, the following are now possible:
In PHP4, it is not possible for a function to return the value of an object and make a method call to the returned object, which, through Zend Engine 2, becomes 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 to New Square ();
}
}

Shapefactorymethod ("Circle")->draw ();
Shapefactorymethod ("Square")->draw ();
?>
Static member variables in static classes 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 that can be invoked from outside the object.
Cases:
<?php
Class Foo {
public static function Astaticmethod () {
// ...
}
}

Foo::astaticmethod ();
?>
The virtual variable $this is not valid in the static method.
instanceof
PHP5 introduces keyword instanceof to determine whether an object is an instance of an object, or derives from an object, or uses an interface.
Cases:
<?php
Class BaseClass {}

$a = new BaseClass;

if ($a instanceof Basicclass) {
echo "Hello World";
}
?>
static function variables
All static variables are now processed at compile time, which allows developers to specify static variables by reference. This change increases efficiency but means it is not possible to indirectly reference static variables.
Allows you to define default values by using parameters that are routed by address in a function
Cases:
<?php
Function my_function (& $var = null) {
if ($var = = null) {
Die ("$var needs to have a value");
}
}
?>
__autoload ()
When an undefined class is initialized, the engine automatically invokes the __autoload () interceptor function. The class name is passed to it as the __autoload () interceptor function unique parameter.
Cases:
<?php
function __autoload ($className) {
Include_once $className. ". PHP";
}

$object = new ClassName;
?>
Overloads of method and property calls
Universal __call (), __get (), and __set () methods can be overloaded with methods and property 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, $a) {
Print "Method $m called:\n";
Var_dump ($a);
return $this->x;
}
}

$foo = new Caller ();
$a = $foo->test (1, "2", 3.4, true);
Var_dump ($a);
?>

Related Article

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.