Home > Article > Backend Development > PHP5 was launched yesterday - new features of PHP5/ZendEngine2.0_PHP Tutorial
Preface Today, I suddenly thought of going to the PHP official website and saw the announcement of the launch of PHP5 at a glance. Although I have seen the preview of PHP5 before, I still carefully read the article on new features of PHP 5/Zend Engine 2.0, and a breath of JAVA came to my face... I have specially translated this article and first published it on the CSDN website for the benefit of readers. . New features of PHP 5/Zend Engine 2.0 Translated by Xu Huanchun sfwebsite@hotmail.com http://www.php.net/zend-engine-2.php New object model The object processing part in PHP has been completely rewritten, with Better performance and more features. In previous versions of PHP, objects were treated the same as built-in variable types (such as integer and string). The disadvantage was that when a variable was assigned to an object or an object was passed as a parameter, you got a copy of the object. In the new version, an object is referenced by its handle, not by its value. (The handle can be thought of as the identifier of the object) Many PHP programmers may not be aware of the "copy quirks" of the previous object model, so the previous PHP program will not need to make any changes, or only make small changes. Runnable Private and Protected Members PHP 5 introduces private and protected member variables, which define when class properties can be accessed. Example: Protected member variables of a class can be accessed in extended classes of this class, while private member variables can only be accessed in this class. Hello; print "MyClass::printHello() " . $this->Bar; print "MyClass::printHello() " . $this->Foo; } } class MyClass2 extends MyClass { protected $Foo; function printHello () { MyClass::printHello(); /* Should print */ print "MyClass2::printHello() " . $this->Hello; /* Shouldnt print out anything */ print "MyClass2::printHello() " . $this->Bar; /* Shouldnt print (not declared)*/ print "MyClass2::printHello() " . $this->Foo; /* Should print */ } } $obj = new MyClass(); print $ obj->Hello; /* Shouldnt print out anything */ print $obj->Bar; /* Shouldnt print out anything */ print $obj->Foo; /* Shouldnt print out anything */ $obj->printHello() ; /* Should print */ $obj = new MyClass2(); print $obj->Hello; /* Shouldnt print out anything */ print $obj->Bar; /* Shouldnt print out anything */ print $obj-> Foo; /* Shouldnt print out anything */ $obj->printHello(); ?> Private and protected methods In PHP 5 (ZEND engine 2), private and protected methods were also introduced. Example: aPrivateMethod(); } } class Bar extends Foo { public function aPublicMethod() { echo "Bar::aPublicMethod() called. "; $this->aProtectedMethod(); } } $o = new Bar; $o->aPublicMethod(); ?> Although "public," "protected" or "protected" are not defined in the user-defined classes or methods in the previous code "private" and other keywords, 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 classes containing abstract methods need to be declared as abstract classes. . Example: test(); ?> The abstract class cannot be instantiated. Although the "abstract" keyword is not defined in the user-defined class or method in the previous code, it can be run without editing. An interface. A class can run any interface list. Example: Although the "interface" keyword is not defined in the user-defined class or method in the previous code, the class type definition can be run without editing. While classes do not need to define types, PHP 5 introduces class type definitions to declare which class you want to pass to a method through parameters. Example: a($b); $a->b($b); ? > These class type definitions are not checked at compile time like some languages that require type predefinition, but at runtime. This means: is equivalent to: This syntax only applies to objects or classes. , does not apply to built-in types. final PHP 5 introduces the "final" keyword to define members or methods that cannot be overridden in subclasses.Example: Although the "final" keyword is not defined in the user-defined class or method in the previous code, it can be run without editing. Object Cloning In PHP 4, when an object is copied, the user cannot determine the copying mechanism. When copying, PHP 4 makes an exact copy of the original object, bit by bit. We don't have to build an exact replica every time. A good example of needing a copying mechanism is when you have an object representing a GTK window, which owns all the resources of that window. When you create a copy, you may need a new window that owns the original window. All properties, but requires owning the resources of the new window. Another example is if you have an object that references another object, and when you copy the parent object, you want to create a new instance of that referenced object so that the copy references it. Copying an object is completed by calling the object's __clone() method: __clone(); ?> When a developer requests to create a new copy of an object, the ZEND engine will check whether the __clone() method is defined. . If undefined, it calls a default __clone() method to copy all properties of the object. If this method is defined, it is responsible for setting the necessary properties in the copy. For convenience, the engine will provide a function to import all properties from the source object, so that it can first get a copy of the source object with values, and only need to overwrite the properties that need to be changed. 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 . " "; $obj = $obj->__clone(); print $obj->id . " "; print $obj->name . " "; print $obj->address . " "; ?> The unified constructor name ZEND engine allows developers to define the constructor of a class. A class with a constructor will first call the constructor when it is created. The constructor is suitable for initialization before the class is officially used. In PHP4 In PHP 4, the name of the constructor method is the same as the class name. Because it is common to call the parent class in a derived class, the way it is handled when a class is moved in a large class inheritance is a bit awkward. When a class is moved to a different parent class, the constructor method name of the parent class must be different, so the statements in the derived class about calling the parent class constructor methods need to be rewritten. 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 constructor. Example For backward compatibility, PHP5 will use the old __construct() method when it cannot be found in the class. The method is the class name to find the constructor. This means that the only possible compatibility issue is if a method name named __construct() has been used in the previous code. Destructor method is very useful. The destructor method can log debugging information, close the database connection, and do other cleanup work. This mechanism is not available in PHP 4, although PHP 5 introduces a similar destructor concept. to that of other object-oriented languages, such as Java: When the last reference to an object is destroyed the objects destructor, which is a class method name %__destruct()% that recieves no parameters, is called before the object is freed from memory. PHP5 introduces a destructor method similar to other object-oriented languages such as Java: when the last reference to the object is cleared, the system will call the destructor named __destruct() before the object is released from memory. Constructor method. Example: name = "MyDestructableClass"; } function __destruct() { print "Destroying " . $this->name . " "; } } $obj = new MyDestructableClass(); ?> Similar to the constructor method, the engine will not call the destructor method of the parent class. To call this method, you need to pass parent::__destruct in the destructor method of the subclass () statement. Constant PHP 5 introduces class constant definition: PHP5 allows expressions in constants, but the expressions in constants will be evaluated at compile time, so the constant cannot change its value at runtime. .