Although PHP5 has not yet been officially released (the development version is already available for download), we can now start to experience the surprises that the new version will bring us. In the following introduction, we will focus on the three major features in PHP5. These three major features are: * New Object Mode * Exception handling (Exceptions) * Namespace (Namespace) Before starting, two points should be declared: * In order to illustrate how to operate, some examples in the article use This is just to improve the readability of the article. * There may be some differences between the parts described in the article and the final release version of PHP5. Before PHP5 is finally officially released, you can download the latest compiled version from http://snaps.php.net at any time to experience PHP5 for yourself. Bringing us these brand new features. The objects in the new object model in PHP5 have been adjusted more systematically and comprehensively, and their current appearance may look somewhat similar to Java. This section focuses on the new object mode in PHP5 and gives some simpler examples to illustrate. Let this section be a new starting point for your PHP5 journey. :) * Constructors and destructors * References to objects * Clone of objects * Private, public and protected modes in objects * Interfaces (Interfaces) * Abstract classes * __call * __set and __get * Static member constructors and analysis Constructor In PHP4, when a function has the same name as an object, this function will become the constructor of the object, and there is no concept of a destructor in PHP4. In PHP5, the constructor is uniformly named __construct, and the concept of destructor is introduced, which is uniformly named __destruct. Example 1: Constructor and destructorx = $x; } function display() { print($this->x); } function __destruct() { print("bye bye"); } } $o1 = new foo(4); $o1->display(); ?> In the above example, when you terminate the call to the foo class, its destructor will be called, and "bye bye" will be output in the above example .As we all know, object references, in PHP4, passing a variable to a function or method actually makes a copy of the variable, which means that what you pass to the function or method is a copy of the variable, unless you use a reference. The symbol "&" is used to declare that a reference is to be made, not a copy. In PHP5, objects always exist in the form of references, and assignment operations in objects are also reference operations. Example 2: Object reference x = $x; } function getX() { return $this->x; } } $o1 = new foo; $o1->setX(4); $o2 = $o1; $o1->setX(5); if($o1->getX() == $o2->getX()) print("Oh my god!"); ?> Cloning of objects is as described above. When an object What should I do if I want to get a copy of the object when it is always called as a reference? PHP5 provides a new feature, which is object cloning, with the syntax __clone. Example 3: Clone of object x = $x; } function getX() { return $this->x; } } $o1 = new foo; $o1->setX(4); $o2 = $o1- >__clone(); $o1->setX(5); if($o1->getX() != $o2->getX()) print("Copies are independant"); ?> The method of object cloning is in other It exists in many application languages, so you don't have to worry about its stability. :) Private, public and protected modes in objects In PHP4, all methods and variables of an object are public, which means that you can operate any of the variables and methods outside an object. PHP5 introduces three new modes for controlling this access permissions: Public, Protected, and Private. Public mode (Public): allows operation control outside the object. Private mode (Private): Only methods within this object are allowed to operate and control it. Protected mode (Protected): Allows this object and its parent object to operate and control it. Example 4: Private, public and protected modes in objects private_foo(); //Ok because we are in the same class we can call private methods print("Im protected"); } private function private_foo() { $this->x = 3; print("Im private"); } } class foo2 extends foo { public function display() { $this->protected_foo(); $this->public_foo(); // $this- >private_foo(); // Invalid! the function is private in the base class } } $x = new foo(); $x->public_foo(); //$x->protected_foo(); //Invalid cannot call protected methods outside the class and derived classes //$x->private_foo(); //Invalid private methods can only be used inside the class $x2 = new foo2(); $x2->display(); ?> Tips: Variables in objects always exist in private form. Directly operating variables in an object is not a good object-oriented programming habit. A better way is to hand over the variables you want to an object's method for processing. Interfaces As we all know, objects in PHP4 support inheritance. To make an object a derived class of another object, you need to use code similar to "class foo extends parent" to control it. In PHP4 and PHP5, an object can only be inherited once, and multiple inheritance is not supported. However, a new term has emerged in PHP5: interface. An interface is a special object without specific processing code. It only defines the names and parameters of some methods. After that, the object can easily use the implement keyword to add the required information. The interfaces are integrated and then specific execution code is added. Example 5: Interface This is very helpful to improve the readability and popularity of the code. From the above example, we can see that the object foo contains two interfaces, displayable and printable. At this time, we can clearly understand You know that object foo must have a display() method and a print() method. As long as you understand the interface part, you can easily operate the object without caring about how the object works internally. To be continued~~~