PHP5 object model [14] - Zend Engine Development

zhaozj2021-02-16  117

Section 14 - ZEND engine development This chapter, Zeev discusses the object model brought by the Zend Engine, especially what is different from the model in the first few versions of PHP. When the summer of 1997, We developed PHP3, we did not plan to make PHPs to have object-oriented ability. There is no idea related to classes and objects. PHP3 is a purely-oriented language. However, in the evening PHP3 alpha version of 1997.8.27 Support for classes. Increase a new feature to PHP, which only requires very little discussion, because people who explore PHP too little. So from August 1997, PHP took the first object-oriented programming language. Step. Indeed, this is just the first step. Because there is only very little related ideas in this design, the support of the object is not strong enough. This version is only a cool way to access the array. Replace the use of $ Foo [Bar "], you can use 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 is not too different from the practice in Example 6.19.Listing 6.18 PHP 3 Object-Oriented Programming PHP3 Object-Oriented Programming

Class Example {var $ value = "some value"; function printvalue () {print $ this-> value;}} $ obj = new example (); $ obj-> printvalue ();?>>

Listing 6.19 PHP 3 structural programming programming in php3 php3

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

$ 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 TO $ A points to the address also change $ b = 7; // Prints 7 Output 7 Print $ A;?>>

Since the object network to each other is the basis of all object-oriented design patterns, this improvement has a significant meaning. When the reference allows for more powerful object-oriented applications, PHP treats the same practice as other types of data. Developers have 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 extremely Pain, see example 6.21 You will understand .Listing 6.21 Problems with Objects in PHP 4 PHP4 Used 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 returnid $ this-> value; 16} 17 18 function getValueFromme () 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 there are three member functions: a value of setting this-> value; A value that returns this-> value; another return this-> value-> me value. But - $ this is not the same thing? Myfoo :: getValue () and myfoo :: getValueFromme () return value is not Same? First, we call CreateObject ("foo"), which will return a Myfoo type object. Then we call Myfoo :: setValue (7). Finally, we call Myfoo :: getValue () and Myfoo :: getValueFromme (), Expect to get a return value 7. Of course, if we get 7 in any case, this example will not be the most meaningless example of this book. So I believe that you have already guessed - we have two 7 Such a result. But what we will get, and more importantly, why? We will get the results 7 and 5. As for why - there are three good reasons. First, see the constructor. Interior of the constructor, we establish a reference between this and this-> me. In other words, this and this-> me are the same thing. But we are in the constructor. When the constructor ends, PHP wants to re-establish the object ( New myfoo's result, 28th line) assigned to $ OBJ. Because the object is not specialized, just like any other data type, assignment X gives Y is a copy of X. That is, Obj will be New A copy of Myfoo, and new myfoo is an object that exists in the constructor. What about it? Because it is a reference, it still does not point to the original object - hefoo still point to the original object - hefo - hefo-iod. Voila-Obj-> ME is no longer the same thing - change one of the other changes. The above is the first reason. There are other reasons similar to the first one. Miracle usually intend to overcome the issue of instantiated objects (Chapter 28 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 no longer be the same. This is the first Two reasons. However, in fact, we can't walk so far - once CreateObject returns $ OBJ, we will destroy the reference (34th line). This is the third reason. So, how do we change? Positive these? There are two options. One is to increase the & symbol in all places, just like Example 6.22 (lines 24, 28, 31, 37). II. If you are lucky, you can forget the above All of this, PHP5 will automatically consider these. If you want to know how PHP5 considers these questions, 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 returnid $ 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; 35} 36 37 $ global_obj = & createObject ("foo"); 38 $ global_obj-> setValue (7); 39 40 Print "Value IS". $ global_obj-> getValue (). "N"; 41 Print "Value IS". $ global_obj-> getValueFromme (). "n"; PHP5 is the first PHP version that looks different to other types of data. From the user's perspective, this proves it Very clear - In PHP5, the object is always transmitted by reference, while other types of data (such as integer, string, array) are passed through a value. Most significant, there is no need to use & symbols to represent by reference To transmit objects. Object-oriented programming has utilized the complex relationship between object networks and objects, which require reference. In some of PHP, you need to display a reference to the reference. So, now the default use to move objects And only replicate the object when you clearly require copying, which is better than before. What is it implemented? Before PHP5, all values ​​have a special structure called ZVAL (Zend Value). These values ​​can save Enter simple values, such as numbers and strings, or complex values ​​such as arrays and objects. When the value is transmitted to the function or from the function Time, these values ​​are copied, and another address in memory is established with the same content. In PHP5, the value is still saved as the ZVAL structure, except for the object. The object exists in a structure called the object store, And each object has a different ID. ZVAL, does not store the object itself, but a pointer to the object. When copying an object's ZVAL structure, for example, we pass an object as a parameter to a function, we No more data. We only keep the same object pointer and notify another ZVAL now to point to the Object Store. Because the object itself is in the Object Store, we will have any changes to it will affect all holdings. Object pointer ZVAL structure. This additional indirect action makes PHP objects like always passing, transparent and efficient ways. Using PHP5, we can now return to sample 6.21, remove all & symbols Everything is still working properly. When we hold a reference function (line 4), one & symbol is not available.

转载请注明原文地址:https://www.9cbs.com/read-8120.html

New Post(0)