Php5.0 description------PHP5 born for object-oriented PHP5 born for object-oriented -------------------------- ------------------- [Abstract] The object-oriented functions of PHP5 currently under development have been greatly enhanced. What kind of language will the next generation of PHP be? Let’s explain in detail the currently released beta release of PHP5. (1) The birth of Zend 2.0 The basic grammar used by PHP4 today is a script compilation engine called the Zend engine. This is one of the reasons for the excellent functions of PHP4, which is a language generated as an improvement to PHP3. Everyone has always believed that the performance of PHP4 has been greatly improved compared to PHP3 based on the original goals, and it occupies a large share in the world of network programming. The Zend company that developed the Zend engine was merged from the companies founded by Zeev Suraski and Andi Gutmans, the main developers of PHP3, while developing PHP4. Zend's name is a combination of Zeev and Andi's names. Zend's business model is to continuously provide the PHP core of the zend engine to open source, while at the same time increasing the benefits of peripheral product development and sales. Businesses based on open source software are considered to be relatively good typical examples among most struggling companies around the world. ■Limitations of PHP4 Thanks to the success of PHP4, the scope of application of this application is gradually becoming wider. There is talk of using PHP for enterprise-level purposes. Therefore, there is a problem that when building a large-scale website, the reusability of the code is very poor. Specifically, the object-oriented performance of PHP4 is very weak, so technical personnel who are accustomed to using Java and other technologies have many complaints about this. By gradually improving the object-oriented performance of PHP4 and significantly changing the basic grammar, the developers achieved the development goal of updating the PHP description method. ■Zend 2.0 development began. Subsequently, developers at the Zend PHP Center announced the idea of the Zend 2.0 engine as the next generation PHP language engine in July 2001. While targeting [Zend Engine version 2.0: Feature Overview and Design] (http://www.zend.com/engine2/ZendEngine-2.0.pdf), the object-oriented performance has been greatly enhanced. The expansion of the current PHP4 Zend engine is exactly the same as that of PHP3 in the past. This means increasing the major version number of the new language engine, clarifying the method goals, and receiving praise from the development team. The development of Ze2, like the previous Zend engine, runs in the open source mode. The latest source code is fully disclosed on CVS, and because it is for open developers, discussions about development are very active. Now Ze2 has been decided to be used in the next version of PHP, PHP5. The final release time has not yet been determined, but if according to the Newsletter released by Zend Company on April 1, 2003, it should be a Beta Release now. (2) New features of PHP5 Next, please take a look at the enhanced performance of PHP5 in order. The first is the most important object-oriented performance. The entity characteristics of classes are being significantly modified. What I'm talking about here is only about the new features of classes. · The reference transition of objects is the default (default) · Introducing restrictions on accessing properties · Introducing restrictions on accessing methods · Abstract classes and abstract methods · Interfaces · Final declaration · Namespace · Intra-class constants · Class variables · Unified builder · Analysis Constructor (Distructor) · Other ancillary features The above content is based on the version information registered on CVS on April 22, 2003. There is also the possibility of changes before the official release. ■Default reference transition of objects In PHP4, when variable $var1 is used as an entity object of class, if $var2 = $var1; then, in $var2, the copy of $var1 is substituted. Obviously, in order for $var2 to point to the same object as $var1, it must be written as $var2 =& $var1, and & must be added as a reference. In PHP5, object substitution will become an automatic reference transition. In other words, $var2=$var1, both point to the same object. If you want to bring in copy like php4, then you will use the method of importing __clone(). $var2 = $var1->__clone(); Here, clone is preceded by two consecutive "_" (this is only a characteristic of the entity of the class) ■Restrictions on accessing attributes are introduced in PHP4 classes, together with attributes and methods Inside, you can freely access anywhere inside and outside the class without restrictions. Therefore, users have no protection against inadvertent changes to attributes. In PHP5, like C++ and Java, three levels of access restrictions, private, protected, and public, are introduced, allowing class designers to limit the use of properties and methods. Here's what the various access restrictions mean. · Public: You can freely make references and changes anywhere inside and outside the class. · Private: You can only make references and changes in the methods of this class. · Protected: You can make references and changes in the methods of this class and another class that inherits this class. Make reference and changes. In addition, access specification can be written in inherited classes. "var" in PHP4 has the same meaning as public as before. Here's an example to see how access restrictions work.PHP code:------------------------------------------------- ---------------------------------- class Hoge1 { private $var1 = A; protected $var2 = B; protected $ var3 = C; function setLower() { $this->var1 = a; $this->var2 = b; $this->var3 = c; } function var1() { return $this->var1; } function var2( ) { return $this->var2; } } function var3() { return $this->var3; } } -------------------------- -------------------------------------------------- ---- In this class, there are three attributes $var1, $var2, $var3. $var1 is declared private, $var2 and $var3 are protected. PHP code here:-------------------------------- -------------------------------------------------- - $hoge=new Hoge1; echo'var1:'.$hoge->var1.” n” ---------------------------- -------------------------------------------------- -- If you try to refer to a private property that is not allowed to be accessed from the outside, the following error will appear: Fatal error: Cannot access private property hoge1::$var1 in /path/to/script.php on line XX For protected $ The same goes for var2. However, because the $hoge method is not private or protected, the following code can operate normally and return the value of the internal private and protected variables. PHP code:------------------------------------------------- ---------------------------------- echo var1: . $hoge->var1() . " "; // var1: A echo var2: . $hoge->var2() . " "; // var2: B echo var3: . $hoge->var3() . " "; // var3: C $hoge->setLower(); echo var1: . $hoge->var1() . " "; // var1: a echo var2: . $hoge->var2() . " "; // var2: b echo var3: . $hoge->var3() . " "; // var3: c ----------------------------------------------- --------------------------------------------- Secondly, in order to see the status of the protected attribute, we Try creating a class Hoge2 that inherits Hoge1 PHP code:--------------------------------------------- --------------------------------------------- class Hoge2 extends Hoge1 { public $ var3 = 3; function d_var1() { return $this->var1; } function d_var2() { return $this->var2; } function d_var3() { return $this->var3; } } ------ -------------------------------------------------- -------------------------- In class Hoge2, only $var3 is declared as public. When the attribute is protected, it can be accessed from subclasses. The restrictions are determined by the attribute declaration of the subclass. In Hoge2, because $var3 is declared public, Hoge2's $var3 can be accessed from anywhere (the entity is Hoge1's $var3). $var1 is private in Hoge1. Therefore, the $var1 of Hoge1 will not be inherited in the Hoge2 subclass. However, a property named $var1 may be made in Hoge2. Therefore, Hoge1::$ must be clearly distinguished. var1 and Hoge2::$var1. PHP code:--------------------------------------------- ------------------------------------------ $hoge = new Hoge2; echo var1: . $hoge->var1 . " "; // var1: // echo var2: . $hoge->var2 . " "; // Error echo var3: . $hoge->var3 . " "; // var3: 3 echo var1: . $hoge->d_var1() . " "; // var1: echo var2: . $hoge->d_var2() . " "; // var2: B echo var3: . $hoge->d_var3() . " "; // var3: 3 ----------------------------------------------- ------------------------------------- $hoge->var1 has nothing to do with Hoge1::var1 variable, so there will be no display, because var2 has protected access restrictions, so if you refer to $var2 directly without passing method, a fatal error will occur ■The restrictions on introducing access methods are the same as above, and they are also classified as private. , protected, public · Public: can be called from anywhere · Private: can only be called from the method of this class · Protected: can only be called from the method of this class and subclass The meaning here is the same as Java and C++. , please don’t get confused. ■Abstract classes and abstract methods support the same abstract classes and abstract methods as Java. Abstract methods only provide the calling method of the method name, and do not provide the entity. The class must declare the class itself abstractly. If you want to directly create an object of the abstract class, the following fatal error will occur. Fatal error: Cannot instantiate abstract class ClassName The actual example of the error is as follows: PHP code:- -------------------------------------------------- ----------------------------- abstract class MyAbstract { abstract public function test(); public function test2() { echo "MyAbstract: :test2() called. "; } } class MyImplement extends MyAbstract { public function test() { echo "MyImplement::test() called. "; } } $obj = new MyImplement; $obj->test(); ?> ---------------------------------- -------------------------------------------------- ■Interface (interface) supports the same interface as Java. The interface is designed and combined to suit the described external call form. The entities of the interface cannot be recorded.In contrast, a class that implements an interface must hold entities corresponding to the methods of the interface. In addition, classes can implement multiple interfaces, so multiple inheritance is possible. PHP code:------------------------------------------------ ----------------