PHP 5 launched yesterday--php 5/zend Engine 2.0 new features _php tutorial

Source: Internet
Author: User
Tags constant definition zend
Objective

Today, I suddenly thought of the PHP official website on a turn, at a glance to see the announcement of the PHP5 launch. Although I have seen the PHP5 before, but still carefully read the PHP 5/zend Engine 2.0 new features of the article, a Java breath is blowing ...
The article will be translated out, 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 drawback that when a variable is assigned to an object or object as a parameter, a copy of the object is obtained. In the new version, the object is referenced by a handle, not by its value. (The handle can be recognized as the identifier of the object)
Many PHP programmers may not be aware of the "copying quirks" of the previous object model, so the previous PHP program will not need to make any changes or make minor changes to run
Private and protected members
PHP 5 introduces private and protected member variables that can define when class properties can be accessed.
Cases
The protected member variable of a class can be accessed in the extension class of the class, and the private member variable can only be accessed in this class.
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:
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 the user-defined class or method in the previous code, but 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 intended to actually run. Classes that contain abstract methods need to be declared as abstract classes.
Cases:
Abstract 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. The "abstract" keyword is not defined in the user-defined class or method in the previous code, but can be run without editing.
Interface
Zend Engine 2.0 introduces an interface. A class can run any list of interfaces.
Example
Cases:
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 can be run without editing.
Class type definition
While preserving a class without defining a type, PHP 5 introduces a class type definition that declares which class you want to pass to a method by argument.
Example
Cases:
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 type definitions are not checked in the compilation, but rather at runtime, unlike some languages that require the type to be predefined. This means that:
function foo (ClassName $object) {
// ...
}
?>
Equivalent to:
function foo ($object) {
if (! ( $object instanceof ClassName)) {
Die ("Argument 1 must is an instance of ClassName");
}
}
?>
This syntax is used only for objects or classes, not for built-in types.

Final
PHP 5 introduces a "final" keyword that defines a member or method that cannot be overridden in a subclass.
Cases:
Class Foo {
Final function bar () {
// ...
}
}
?>
The "final" keyword is not defined in the user-defined class or method in the previous code, but can be run without editing.
Object cloning
PHP 4 The user cannot determine the mechanism of the copy when the object is copied. When copying, PHP 4 copies only one copy of the original object.
We don't always have to create exactly the same copy. A good example of a replication mechanism is when there is an object that represents a GTK window, it has all the resources of that window, and when you create a copy, you may need a new window that has all the properties of the original window, but requires a resource with a 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 reference object so that the replica references it.
A copy of an object is done by invoking the __clone () method of the object:
$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 undefined, it invokes a default __clone () method to copy all 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 and only need to overwrite the property that needs to be changed.
Cases:
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 constructor Method Name
The Zend Engine allows developers to define how classes are constructed. A class with a constructed method will first call the construction method when it is created, and the construction method applies to the initialization that was made before the class was formally used.
In PHP4, the constructor method has the same name as the class name. Because it is common to call the parent class in a derived class, processing is a bit clumsy when the class is moved in a large class inheritance in PHP4. When a derived class is moved to a different parent class, the constructor name of the parent class must be different, so that the statement in the derived class about calling the parent class construction method needs to be overwritten.
PHP 5 introduces a standard-of-the-declaring constructor methods by calling them by the name __construct ().
PHP5 introduces method name __construct () to define the construction method.
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 ();
?>
Backwards compatibility, PHP5 when the class cannot find the __construct () method, the constructor is found by the old method, which is the class name. 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 debug information, close database connection, and do other cleanup work. There is no such mechanism in PHP4, although PHP supports registering functions that need to be run at the end of the request.
PHP 5 introduces a destructor concept similar to then of other object-oriented languages, such as Java:when the last Refe Rence to an object is destroyed the object's destructor, which is a class method name%__destruct ()% that Recieves no para Meters, is called before the object was freed from memory.
PHP5 introduces a destructor similar to other object-oriented languages such as the Java language: When the last reference to the object is cleared, the system will call a destructor called __destruct () before the object is freed from memory.
Cases:
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 will not call the parent class's destructor, and in order to invoke the method, you need to make a call through the parent::__destruct () statement in the destructor of the child class.
Constant
PHP 5 introduces a class constant definition:
Class Foo {
CONST CONSTANT = "constant";
}

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

PHP5 allows an expression in a constant, but the expression in the compile constant is evaluated., so a constant cannot change its value in the run.
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 can be 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, and PHP5 references an exception-handling model similar to other languages.
Cases:
Class Myexceptionfoo extends Exception {
function __construct ($exception) {
Parent::__construct ($exception);
}
}

try {
throw new Myexceptionfoo ("Hello");
} catch (MyException $exception) {
Print $exception->getmessage ();
}
?>
The user-defined class or method in the previous code did not define ' catch ', ' throw ' and ' try ' keywords, but could run without editing.
function return Object value
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 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:
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 now be initialized
Example
class Foo {
static $my _static = 5;
}

Print foo:: $my _static;
?>
Static methods
PHP5 introduces the keyword ' static ' to define a static method so that it can be called from outside the object.
Cases:
Class Foo {
public static function Astaticmethod () {
// ...
}
}

Foo::astaticmethod ();
?>
The virtual variable $this is not valid in a static method.
instanceof
PHP5 introduces the keyword instanceof to determine whether an object is an instance of an object, or a derivation of an object, or an interface is used.
Cases:
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 the developer to specify static variables by reference. This change improves efficiency but means that it is not possible to indirectly reference a static variable.
Allows you to define default values in a function by means of an address-by-route parameter
Cases:
Function my_function (& $var = null) {
if ($var = = = null) {
Die (' $var needs to has a value ');
}
}
?>
__autoload ()
When an undefined class is initialized, the engine automatically calls the __autoload () interceptor function. The class name is passed to it as the unique parameter of the __autoload () interceptor function.
Cases:
function __autoload ($className) {
Include_once $className. ". PHP";
}

$object = new ClassName;
?>
Overloads for method and property calls
The generic __call (), __get (), and __set () methods can be overloaded with methods and property calls.

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, $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);
?>

http://www.bkjia.com/PHPjc/314178.html www.bkjia.com true http://www.bkjia.com/PHPjc/314178.html techarticle Foreword today suddenly think of PHP official website on a turn, at a glance saw PHP5 launch notice. Although I have seen the PHP5 before, but also carefully read the PHP 5/zend Engine 2.0 new features of the article ...

  • 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.