Php5/zendengine2 's Improved _php tutorial

Source: Internet
Author: User
Tags getmessage
The 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 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 copy is obtained. In the new version,
Objects are referenced by a handle, not by the value of the object (the handle is imagined as the object's identifier).
Many PHP programmers may not be aware of the old object model "copying quirks", so most of the previous PHP programs will not need to make any changes
Can be run, or only with minimal changes.

Private and protected members
PHP 5 introduces private and protected member variables that can define the class properties of a visualization.
Example
Protected member variables can be accessed in subclasses of the class, and private member variables can only be accessed in the owning class.
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; /* Do not output any content, the following is similar to */
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 methods and protection methods are also introduced.
Example:
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 ();
?>
The user-defined class or method in the previous code did not define keywords such as "public," "protected" or "private", but could run without modification.

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

Although the "interface" keyword is not defined in the user-defined class or method in the old code, it can work without modification.

Class type hint (class type Hints)
While a reserved class does not need to define a type, PHP 5 introduces a class type hint to declare that it expects to pass the object's class through parameters to a method.
For example:
A ($b); $a->b ($b);? >
These class type hints are not checked in the compilation, as in some languages that require type definitions, but are checked at run time. This means that:

is equivalent to:

This syntax is used only for objects or classes, not for built-in (built-in) types.

Final keyword (final)
PHP 5 Introduces the "final" keyword to define 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 previously written code, but can be run without modification.
Objects cloning (object cloning)
PHP 4 The user cannot determine which copy constructor to run when the object is copied. When copying, PHP 4 is based on the properties of the object
One by one copies the same copy.
It's not always what we want to create an exact replica of each time. An example of a good copy construction is when there are
An object that represents a GTK window that has all the resources of that window, and when you create a copy, you may need a
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 a
Object refers to another object, and when you copy the parent object, you want to create a new instance of the reference object to have a separate copy of the copy.
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 has been defined. If you do not define
, it invokes a default __clone () method to replicate all properties of the object. If the method is defined, the method is responsible for
Set the necessary properties in the copy. For ease of use, the engine provides a function to import all the properties from the source object so that it can be
To get a copy of the source object with a value first, and then just 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 approach to construction
The Zend Engine allows developers to define how classes are constructed. A class with a constructor method first calls the constructor method when it is created, constructs
method is intended for 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 call the parent class in a derived class, it causes the
In PHP4, when a class moves in a large class inheritance, the processing is a bit awkward. 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.
PHP5 introduces a standard way of defining construction methods, 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 backwards compatibility, when the PHP5 class cannot find the __construct () method, the old method is the class name
To find the constructor method. This means that the only possible compatibility issue is in the previous code that has been used
A method name named __construct ().

destructor method
It is very useful to define a destructor method. The destruction method can record debug information, close database connection, and do other cleanup.
Job. There is no such mechanism in PHP4, although PHP supports registering functions that need to be run at the end of the request.
PHP5 introduces a destructor similar to other object-oriented languages like the Java language: When the last reference to the object is cleared,
The system will call a destructor named __destruct () before the object is freed from 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 will not call the parent class's destructor, and for calling the method, you need to
Class is called by the Parent::__destruct () statement in the destructor method of the

Constant
PHP 5 introduces a class constant (Per-class constants) Definition:
Class Foo {
CONST CONSTANT = "constant";
}

echo "foo::constant =". Foo::constant. "\ n";
?>
PHP5 allows an expression to be included in a constant, but the expression in the compile constant is evaluated,
Therefore, a constant cannot change its value in the run.
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 it without modification.

Exception (Exceptions)
No exception is handled in PHP4, and PHP5 introduces an exception-handling model similar to that of other languages.
Class Myexceptionfoo extends Exception {
function __construct ($exception) {
Parent::__construct ($exception);
}
}

try {
throw new Myexceptionfoo ("Hello");
} catch (Myexceptionfoo $exception) {
Print $exception->getmessage ();
}
?>

The user-defined class or method in the previous code does not define ' catch ', ' throw ' and ' try ' keywords, but does not need to be modified
You can run it.

function return Object value
In PHP4, a function cannot return the value of an object and make a method call to the returned object, with Zend Engine 2
(Zend Engine 2), the following call 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 a static class can be initialized
For example:
class Foo {
static $my _static = 5;
}

Print foo:: $my _static;
?>


static method (Methods)
PHP5 introduces the keyword ' static ' to define a static method so that it can be called from outside the object.
For example:

Class Foo {
public static function Astaticmethod () {
// ...
}
}

Foo::astaticmethod ();
?>

The virtual variable $this is not valid in a method that is defined as static.


instanceof

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

Example:

Class BaseClass {}

$a = new BaseClass;

if ($a instanceof Basicclass) {
echo "Hello World";
}
?>



static function variable (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.


Parameters passed by reference in a function allow default values
For example:

Function my_function (& $var = null) {
if ($var = = = null) {
Die (' $var needs to has a value ');
}
}
?>


__autoload ()


When initializing an undefined class, the __autoload () intercept function (Interceptor functions) is automatically
Use. The class name is passed to it as the unique parameter of the __autoload () intercept function.
For example:
function __autoload ($className) {
Include_once $className. ". PHP";
}

$object = new ClassName;
?>


Overloads for method and property calls
All method invocations and property accesses can be overloaded by Universal __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, $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/313997.html www.bkjia.com true http://www.bkjia.com/PHPjc/313997.html techarticle The object-handling part of the new object model PHP has been completely rewritten, with better performance and more functionality. In previous versions of PHP, objects were treated as primitive simple types such as inte ...

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