PHP 5 / Zend Engine 2.0 improvements
Warton Translation 2003-09-12 Chongqing
In the past two years, I didn't use PHP to write the program. Today, I have to use PHP, I checked it online, I saw PHP5, and I was very interesting between the time, so I translated this article.
The article is from http://www.php.net/.
New object model
The object processing section in the PHP has been completely rewritten, with better performance and more features. In the previous PHP version, the object is used as an original simple type.
(Such as Integer and String) to process, the disadvantage of this method is that the object copy is obtained when the variable is assigned or transmitted as a parameter. In the new version,
The object is referenced by the handle instead of the value of the object (the handle is imagined to be an identifier).
Many PHP programmers may not be aware of the "Copying Quirks" of the old object model, so most PHP programs will not need to make any changes.
It can be run, or only very few changes.
Private and protect members
PHP 5 introduces private and protected member variables that can define visual class properties.
Example
Protection member variables are accessed in the subclass of such classes, while private member variables can only be accessed in the class.
phpclass myclass {private $ hello = "Hello, World! / N"; protected $ bar = "Hello, foo! / n"; protected $ foo = "Hello, bar! / n"; function printhello () {print "Myclass :: Printhello ()". $ This-> 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; / * does not output any content, the following class * / print $ obj-> bar; / * Shouldn't print out anything * / print $ @ / print $ Obj-> foo; / * shouldn't print out anything * / $ obj-> printhello (); / * shop 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 method
In PHP 5 (Zend Engine 2), the private method and protection method are also introduced.
Example:
Phpclass 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-> AprotacectedMethod ();}} $ o = new bar; $ o- > ApublicMethod ();?> Although the user-defined class or method in previous code does not define keywords such as "PUBLIC," Protected "or" Private ", but it is not necessary to modify it.
Abstract class and method
PHP 5 also introduces abstractions and methods. Abstract methods only declare the "symbol" of the method without providing it. A class containing an abstract method needs to be declared as "Abstract".
E.g:
?
Abstract classes cannot be instantiated.
Although the "Abstract" keyword or method in the old code is not defined, it can be run without modification.
Interfaces
Zend Engine 2.0 introduces an interface. A class can implement an arbitrary interface list.
E.g:
phpinterface throwable {public function getMessage ();} Class Exception Implements throwable {public function getMessage () {// ...}?>
The user-defined class or method in the old code is not defined in the "interface" keyword, but it is possible to run normally without modification.
Class Type Tips (Class Type Hint)
At the same time as the retention class does not need to define a type, PHP 5 introduces class type prompt to declare to provide a method of passing the object's class to a method.
E.g:
phpinterface foo {function a (foo $ foo);} interface bar {Function B (bar $ bar);} Class Fooction A (Foo $ foo) {// ...} Function B ( Bar $ bar) {// ...}} $ a = new foobar; $ b = new foobar; $ A-> A ($ b); $ A-> b ($ b);?>>>>>>>>>>
These types of type tips are not checked in compilation as some languages that require type definitions, but check at runtime. This means:
phpfunction foo (ClassName $ Object) {// ...}?> is equivalent to:
phpfunction foo ($ Object) {if ($ Object InstanceOf classname) {Die ("argument 1 must be an instance of classname);}}?>
This syntax is only used for objects or classes, which is not suitable for built-in type.
Final Keywords (FINAL)
PHP 5 introduces "Final" keyword to define members or methods that cannot be overwritten in subclasses.
example:
PHP
Class foo {finction bar () {// ...}}?>
Although the "final" keyword or method in the user-written code is not defined, it can be run without modification.
Object cloning
PHP 4 When the object is replicated, the user cannot determine that the copy constructor is running. At the time of replication, PHP 4 is based on the object of the object.
One place replicates a same replica.
Each time you have to build a exact amount of replica is not always what we want. A good copy constructor is that when there is
A object representing a GTK window, which has all resources of the window, when you create a copy, you may need one
A new window, it has all the properties of the original window, but you need a resource with a new window. Another example is that you have one
Objects references another object, when you copy the parent object, you want to establish a new instance of the reference object to make the replica have a separate copy.
Copy to an object through the __clone () method of the object:
PHP
$ COPY_OF_OBJECT
= $ Object -> __clone ();
?>
When the developer requests a new copy of an object, the Zend engine checks if the __clone () method has been defined. If not defined
If it calls a default __clone () method to copy all the properties of the object. If this method is defined, the method is responsible
Set the necessary properties in the copy. For easy use, the engine provides a function to import all properties from the source object so that it can
First get a copy of the value of the value, and then only the properties that need to be changed can be overwritten.
example:
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";
?> Unified constructor
The Zend Engine allows the developer to define the constructor of the class. The class with a construction method will first call the construction method, construct
The method is suitable for initialization pre-use of this class.
In PHP4, the name of the constructor is the same as the class name. Since the process of calling the parent class is common in the derived class, it is caused
PHP4 is moving when moving in a large class inheritance, is a bit awkward. When a derived class is moved to a different
In the parent class, the texture method of the parent class is inevitably different, and the statement in the derived class is related to the statement of the parent class constructor needs to be rewritten.
PHP5 introduces a standard way to define the construction method, defined by calling their __construct ().
Example:
?
To be backward compatible, when the PHP5 class cannot find the __construct () method, the old method is also a class name.
To find the constructor. This means that the only compatibility problem is that it has already been used in the previous code.
A method name called __construct ().
Destructure method
Define the destructor is very useful. The destructure method can record debugging information, turn off database connections, as well as other sweeping
the work. There is no such mechanism in PHP4, although PHP has supported registration to run function when the request is completed.
PHP5 introduces a similar method similar to other object-oriented languages such as Java language: When the final one of this object is cleared,
The system will call a description method called __destruct () before the object is released from memory.
Example:
Phpclass 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 call the destructor of the parent class, to call the method, you need to be in the child
The sect of the class is called by the Parent :: __ destruct () statement.
constant
PHP 5 introduces a PER-CLASS Constants definition:
phpclass foo {constant = "constant";} echo "foo :: constant =". foo :: constant. "/ n";?>>
[Next]