15th Section--zend Engine Development-Classes and Objects in php5_php

Source: Internet
Author: User
Keywords Engine development 15 object PHP function reference face this-
| = This is haohappy read < >
| = Notes in Classes and objects Chapter
| = Translation-based + personal experience
| = Please do not reprint in order to avoid the unnecessary trouble that may occur.
| = Welcome criticism, hope and all the PHP enthusiasts to progress together!
| = PHP5 Research Center: http://blog.csdn.net/haohappy2004

section 15th development of the--zend engine

In the last section of this chapter, Zeev discusses the object model that the Zend engine brings, especially when it differs from the model in the first few versions of PHP.
When we developed PHP3 in the summer of 1997, we had no plans to make PHP capable of object-oriented. There were no ideas about classes and objects at the time. PHP3 is a purely process-oriented language. However, support for the class was added to the PHP3 Alpha version of the 1997.8.27 night. Adding a new feature to PHP was only a minimal discussion, because there were too few people to explore PHP at the time. Since August 1997, PHP has taken the first step toward an object-oriented programming language.

Indeed, this is only the first step. Because there are very few related ideas in this design, support for objects is not strong enough. Using objects in this version is only a cool way to access an array. Instead of using $foo["bar", you can use a $foo->bar that looks prettier. The main advantage of an object-oriented approach is to store functionality through member functions or methods. A typical code block is shown in example 6.18. But it's not much different from the approach in example 6.19.

Listing 6.18 PHP 3 object-oriented Programming PHP3 in object-oriented programming
   value;        }    }    $obj = new Example ();    

Listing 6.19 PHP 3 structural programming PHP3 PHP3 structured programming

Above we write two lines of code in the class, or display the array to the function. But given that the two options in PHP3 are not any different, we can still access the array only as a "grammatical whitewash" of the object model.

People who want to use PHP for object-oriented development, especially those who want to work with design patterns, quickly find themselves at a wall. Fortunately, there were not many people who wanted to use PHP for object-oriented development at the time (PHP3).

PHP4 changed the situation. The new version introduces the concept of reference (reference), which allows different identifiers of PHP to point to the same address in memory. This means that you can use two or more names to name the same variable, as in example 6.20.

Listing 6.20 PHP 4 references PHP4 references

Since building an object network to each other is the basis of all object-oriented design patterns, this improvement is of great significance. When references allow more powerful object-oriented applications to be built, The same approach that PHP treats objects and other types of data brings great pain to developers. Just like any PHP4 programmer will tell you, the application will encounter Wtma (way Too many ampersands &) syndrome. If you want to build a practical application, you will feel extremely painful, see example 6.21 you understand.

Listing 6.21 problems with objects in PHP 4 PHP4 problems with objects
1 class Myfoo {2 function Myfoo () 3 {4 $this->me = & $this; 5 $this->val UE = 5; 6} 7 8 function SetValue ($val) 9 {$this->value = $val; one}        Nction GetValue () {19 return $this->value;) + function Getvaluefromme {return $this->me->value;} createobject ($class _type) ($class _type) {case "foo": $obj = new Myfoo (); 2 9 break; Case "Bar": $obj = new Mybar (); break; The return $obj; The PNs $global _obj = CreateObject ("foo"); _obj->setvalue $global (7); Max print "Value is". $global _obj->getvalue (). "\ n"; Print "Value is". $global _obj->getvaluefromme ().  "\ n";

Let's take a step-by-step discussion. First, there is a Myfoo class. In the constructor, we give $this->me a reference and set
We have three other member functions: A value that sets This->value, one that returns This->value, and another that returns a value of This->value->me. But--$this not the same thing? Are the values returned by Myfoo::getvalue () and Myfoo::getvaluefromme () not the same?

First, we call CreateObject ("Foo"), which returns an object of type Myfoo. Then we call Myfoo::setvalue (7). Finally, we call Myfoo::getvalue () and Myfoo::getvaluefromme (), expecting to get a return value of 7.
Of course, if we get 7 in any case, the above example will not be the least meaningful example in this book. So I'm sure you've guessed it-we don't get two or 7 of these results.

But what results will we get, and more importantly, why?

The results we will get are 7 and 5, respectively. As to why —--have three good reasons.

First, look at the constructor. When inside a constructor, we establish a reference between this and this->me. In other words, this and this->me are the same thing. But we are inside the constructor. When the constructor ends, PHP will re-establish the object (the result of new Myfoo, line 28th) is assigned to $obj. Because the object is not specialized, just like any other data type, assigning x to Y means that Y is a copy of X. In other words, obj will be a copy of new Myfoo, and new Myfoo is an object that exists in the constructor. What about Obj->me? Because it is a reference, it still points to the original object-this. Voila-obj and Obj->me are no longer the same thing-change one another unchanged.

The above is the first reason. There are other reasons similar to the first article. Miraculously we intend to overcome the problem of instantiating objects (line 28th). Once we assign the value returned by CreateObject to Global_object, we still have to hit the same problem-global_object will become a copy of the return value, and again, Global_object and global_object- >me will not be the same. That's the second reason.

But, in fact, we're not going that far-once CreateObject returns to $obj, we'll break the reference (line 34th). That's the third reason.

So, how do we correct this? There are two options. One is to add & symbols in all places, as in example 6.22 (24th, 28, 31, 37 lines). Two. If you are lucky enough to use the PHP5, you can forget all of this, PHP5 will automatically consider these for you. If you want to know how PHP5 is thinking about these issues, read on.

Listing 6.22 Wtma syndrome in PHP 4 PHP4 Wtma syndrome
1 class Myfoo {2 function Myfoo () 3 {4 $this->me = & $this; 5 $this->val UE = 2; 6} 7 8 function SetValue ($val) 9 {$this->value = $val; one}        Nction GetValue () {19 return $this->value;) + function Getvaluefromme {return $this->me->value; 21} 22}; function &createobject ($class _type) {($class _type) {v CA Se "foo": $obj =& new Myfoo (); break; Case "Bar": $obj =& New Mybar (); break; The return $obj; $global _obj =& CreateObject ("foo"); _obj->setvalue $global (7); Max print "Value is". $global _obj->getvalue (). "\ n"; Print "ValuE is ". $global _obj->getvaluefromme ().  "\ n";

PHP5 is the first PHP version that sees an object as different from other types of data. From the user's point of view, this proves that it is very clear-in PHP5, objects are always passed by reference, while other types of data (such as Integer,string,array) are passed by value. Most notably, there is no need to use the & notation to convey objects by reference.

Object-oriented programming extensively exploits the complex relationships between object networks and objects, all of which require references. In previous versions of PHP, it was necessary to indicate the reference. As a result, objects are now moved by default with references, and objects are copied only when explicitly required for replication, which is better than before.

How is it implemented?

Before PHP5, all values exist in a special structure called Zval (Zend value). These values can be stored in simple values such as numbers and strings, or complex values such as arrays and objects. When a value is passed to a function or returned from a function, these values are copied and a structure with the same content is created at another address in memory.

In PHP5, the value is still stored in the zval structure, except for the object. Objects exist in a structure called the object store, and each object has a different ID. In Zval, the object itself is not stored, but a pointer to the object is saved. When copying a zval structure that holds an object, for example, we pass an object as a parameter to a function, we no longer copy any data. We just keep the same object pointer and notify the object Store by another zval that the specific objects are pointing to. Because the object itself is in object Store, any changes we make to it will affect all the zval structures that hold the pointer to that object. This additional indirect effect makes the PHP object look like it is always passed by reference, in a transparent and efficient manner.

With PHP5, we can now go back to Example 6.21, remove all the & symbols, and all the code will still work. When we hold a reference in the constructor (line 4th), A & symbol is not used.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.