Section 15 - Development of ZEND Engine - Classes and Objects in PHP5 [15]
Author: Leon Atkinson translation: Haohappy Source: Beyond the PHP / * ------------------------------------ ------------------------------------------ | = This article is HaohappY << CORE PHP Programming >> | = Classes and Objects Chapter Note | = Translate Main Personal Experience | = To avoid unnecessary troubles, please do not reprint, thank you | = Welcome criticism, hope and all PHP enthusiasts progress together! ----------------------------------------- ---------------------------------- * / Article 15 - ZEND engine development This chapter The last section, Zeev discusses the object model brought by the Zend Engine, specifically mentioned that it is different from the model in the first few versions of PHP. When we developed PHP3 in 1997, we did not plan to make PHP With object-oriented ability. There is no idea related to classes and objects. PHP3 is a purely-oriented language. However, in PHP3 Alpha version of 1997.8.27, PHP3 Alpha has added support. Add a new feature to give PHP, only a little discussion at the time, because people who explored PHPs too little. So from August 1997, PHP took the first step towards object-oriented programming languages. Indeed, this is just the first step. Because Only very little related ideas in this design are not strong enough for objects. This version is just a cool way to access arrays. Use $ foo ["bar"], you can use it. More beautiful $ foo-> bar. The main advantage of object-oriented methods is to store features through member functions or methods. Example 6.18 shows a typical code block. But it and the practice in Example 6.19 are not too Big Different .Listing 6.18 PHP 3 Object-Oriented Programming PHP3 Object-Oriented Programming PHP Class Example {Var $ VALUE = "Some Value"; Function PrintValue () {Print $ this-> value;}}} $} = new EXAMPLE (); $ OBJ-> PrintValue ();?> Listing 6.19 PHP 3 structural programming php3 Structured programming PHP Function PrintValue ($ Arr) {Print $ Arr ["Value"];} Function Createexample () {$ Arr ["Value" = "Some Value"; $ arr ["PrintValue"] = "PrintValue"; Return $ Arr;} $ arr = creteexample (); // USE PHP's Indirect Reference $ Arr ["PrintValue"] ($ arr);?>
We write two lines of code in the class, or send the array to the function. But considering that the two options in PHP3 are not different, we can still only use the object model as a "grammar" Visit an array. Want to use PHP to develop people, especially those who want to use design patterns, soon discover them to touch the wall. Fortunately, at the time (PHP3 era) not too many people want to use PHP to do Object development .PHP4 changes this. The new version brings the concept of reference (Reference), which allows PHP's different identifiers to point to the same address in memory. This means you can use two or more Name to name the same variable, like an example 6.20.Listing 6.20 PHP 4 References PHP4 reference PHP $ a = 5; // $ b Points to the Same Place in Memory AS $ A $ B with $ A Point to the same address in memory $ b = & $ a; // We're Changing $ b, Since $ a is pointing to change $ b, pointing address change // the Same Place - IT Changes Too $ a point to the address Also change $ b = 7; // PRINTS 7 output 7 Print $ A;?> This improvement has a significant significance because it is the basis for object-oriented objects that point to each other, this improvement is very significant. This improvement is very significant. When reference is allowed to establish more Powerful object-oriented applications, PHP treatments and other types of data the same approach to developers great pain. Just like any PHP4 programmer will tell you, the app will encounter WTMA (Way Too Many ampersands) Excessive &) syndrome. If you want to build an actual application, you will feel very painful, see Example 6.21 You will understand .Listing 6.21 Problems With Objects in PHP 4 PHP4 User Objects 1 Class Myfoo {2 Function Myfoo () 3 {4 $ this-> me = & $ this; 5 $ this-> value = 5; 6} 7 8 Function setValue ($ val) 9 {10 $ this-> value = $ val; 11} 12 13 Function getValue () 14 {15 return $ this-> value; 16} 17 18 Function Getva Luefromme () 19 {20 return $ this-> me-> value; 21} 22} 23 24 function creteObject ($ class_type) 25 {26 Switch ($ class_type) {27 case "foo": 28 $ obj = new myfoo 29 Break; 30 case "bar": 31 $ obj = new mybar (); 32 break; 33} 34 Return $ OBJ; 35} 36 37 $ global_obj = creteObject ("foo"); 38 $ global_obj-> setValue (7); 39 40 Print "Value IS". $ Global_obj-> getValue (). "N"; 41 print "value is". $ Global_obj-> getValueFromme (). "N"; let's discuss it step by step. First, there is a Myfoo class. In the constructor, we give $ this-> me a reference, and set us some other three member functions: a value setting this-> value; one returns this-> Value Value; another return this-> value->
The value of ME is not the same thing? Myfoo :: getValue () and myfoo :: getValueFromme () return the value not the same? First, we call CreateObject ("foo"), this will return A Myfoo type object. Then we call myfoo :: setValue (7). Finally, we call Myfoo :: getValue () and myfoo :: getValueFromme (), expect to get the return value 7. Of course, if we are in any case Get 7, the above example will not be the most meaningless example of this book. So I believe that you have already guessed - we have to get two 7 results. But what we will get, and more importantly, why? • The results of us will be 7 and 5. As for why - there are three good reasons. First, see constructor. Inside the constructor, we establish a reference between this and this-> me. Say, this and this-> me are the same thing. But we are in the constructor. When the constructor ends, PHP is to re-establish the object (New myfoo's result, line 28) assign $ Obj. Because the object is not special Treatment, just like any other data type, assignment X gives Y means Y is a copy of X. That is, OBJ will be a copy of New Myfoo, and new myfoo is an object that exists in constructor. OBJ -> ME How? Because it is a reference, it is still correct to point to the original object. Voila-obj and obj-> me is no longer the same thing - change one of the other. Or change. Or above It is the first reason. There are other reasons similar to the first one. Miracle we plan to overcome the issue of instantiated objects (28th line). Once we assume the value returned by CreateObject to global_Object, we still have to hit The same problem - Global_Object will turn a copy of the return value, and again, global_object and global_object-> Me will not be the same. This is the second reason. However, in fact, we can't walk so far - once creteObject Return $ OBJ, we will destroy the reference (line 34). This is the third reason. So, how do we correct this? One is to increase the & symbol in all places, like an example 6.22 (24th, 28, 31, 37 lines). II. If you are lucky to use PHP5, you can forget all the above, PHP5 will automatically consider these. If you want to know how PHP5 is how to consider these problems, Continue reading .Listing 6.22 WTMA SYNDROME IN PHP 4 PHP4 WTMA Syndrome 1 Class Myfoo {2 function myfoo () 3 {4 $ this-> me = & $ this; 5 $ this-> value = 2; 6} 7 8 Function SetValue ($ VAL) 9 {10 $ this-> value = $ val; 11} 12 13 function getValue () 14 {15 return $ this-> value; 16} 17 18 function getValueFromme () 19 {20 returnirc $ this-> me-> value; 21} 22}; 23 24 Function & CreateObject ($ class_type) 25 {26 Switch ($ Class_Type) {27 Case "foo": 28 $ obj = & new myfoo (); 29 Break; 30 case "bar": 31 $ obj = & new mybar (); 32 break; 33} 34 Return $ OBJ; 35} 36 37 $ global_obj = & createObject ("foo"); 38 $ global_obj-> setValue (7) ; 39 40 Print