Section 15th-Development of the Zend Engine

Source: Internet
Author: User

/*
+ ------------------------------------------------------------------------------- +
| = Read this article for Haohappy <Core PHP Programming>
| = Notes in the Classes and Objects chapter
| = Translation-oriented + personal experiences
| = Do not repost it to avoid unnecessary troubles. Thank you.
| = Thank you for your criticism and hope to make progress together with all PHP fans!
| = PHP5 site: http://blog.csdn.net/haohappy2004
+ ------------------------------------------------------------------------------- +
*/
Section 15th -- Development of the Zend Engine
In the last section of this chapter, Zeev discusses the object model brought about by the Zend engine, especially the differences between Zeev and the models in the previous versions of PHP.
When we developed PHP3 in the summer of 1997, we had no plans to make PHP object-oriented. at that time, there was no idea about classes and objects. PHP3 is a purely process-oriented language. however, the class support is added to the PHP3 alpha version on the evening of 1997.8.27. A new feature was added to PHP, which was rarely discussed at the time, because there were too few people exploring PHP. since August 1997, PHP has taken the first step towards object-oriented programming language.
Indeed, this is only the first step. there are only a few related ideas in this design, and the support for objects is not strong enough. in this version, using objects is only a cool way to access arrays. instead of using $ foo ["bar"], you can use $ foo-> bar that looks more beautiful. the main advantage of object-oriented methods is to store functions through member functions or methods. in Example 6.18, a typical code block is displayed. however, it is not very different from the practice in example 6.19.
Listing 6.18 PHP 3 object-oriented programming Object-Oriented programming in PHP3 Copy codeThe Code is as follows: <? Php
Class Example
{
Var $ value = "some value ";
Function PrintValue ()
{
Print $ this-> value;
}
}
$ Obj = new Example ();
$ Obj-> PrintValue ();
?>

Structured programming in Listing 6.19 PHP 3 structural programming PHP3 PHP3Copy codeThe Code is as follows: <? Php
Function PrintValue ($ arr)
{
Print $ arr ["value"];
}
Function CreateExample ()
{
$ Arr ["value"] = "some value ";
$ Arr ["PrintValue"] = "PrintValue ";
Return $ arr;
}
$ Arr = CreateExample ();
// Use PHP's indirect reference
$ Arr ["PrintValue"] ($ arr );
?>

The above two lines of code are written in the class, or an array is passed to the function explicitly. however, considering that the two options in PHP3 are not different, we can still use the object model as a "syntactic whitewashing" to access the array.
People who want to use PHP for object-oriented development, especially those who want to use design patterns, will soon find them hitting the wall. fortunately, not many people in the PHP3 era wanted to use PHP for object-oriented development.
PHP4 has changed this situation. the new version introduces the reference concept, which allows different PHP identifiers to point to the same address in the memory. this means that you can use two or more names to name the same variable, as in example 6.20.
Reference in Listing 6.20 PHP 4 references PHP4Copy codeThe Code is as follows: <? Php
$ A = 5;
// $ B points to the same place in memory as $ a $ B and $ a point to the same address in memory
$ B = & $;
// We re changing $ B, since $ a is pointing to change $ B, pointing to address change
// The same place-it changes too $ a points to the address also changed
$ B = 7;
// Prints 7 output 7
Print $;
?>

Since building an object network pointing to each other is the basis of all object-oriented design patterns, this improvement is of great significance. when reference allows more powerful object-oriented applications, PHP treats objects and other types of data in the same way, causing great pain for developers. as any PHP4 programmer will tell you, applications will suffer from WTMA (Way Too Many Ampersands &) syndrome. if you want to build a practical application, you will feel extremely painful. Look at example 6.21 and you will understand.
Listing 6.21 Problems with objects in PHP 4 PHP4Copy codeThe Code is as follows: 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 getValueFromMe ()
19 {
20 return $ 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 "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
We have three other member functions: one sets the value of this-> value, one returns the value of this-> value, and the other returns the value of this-> value-> me. but isn't -- $ this the same thing? Is the returned values of MyFoo: getValue () and MyFoo: getValueFromMe () different?
First, we call CreateObject ("foo"), which returns an object of the MyFoo type. then we call MyFoo: setValue (7 ). finally, we call MyFoo: getValue () and MyFoo: getValueFromMe () and expect the return value 7.
Of course, if we get 7 under any circumstances, the above example will not be the most meaningless example in this book. So I believe you have guessed that-we cannot get the result of two 7.
But what results will we get, and more importantly, why?
The results we will get are 7 and 5 respectively. As for why-there are three good reasons.
First, let's look at the constructor. within the constructor, we create 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 will re-establish the object (new MyFoo result, row 28th) and allocate it to $ obj. because the object is not specially treated, just like any other data type, assigning X to Y means that Y is a copy of X. that is to say, obj will be a copy of new MyFoo, while new MyFoo is an object that exists in the constructor. 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 of them and the other remains unchanged.
The above is the first reason. there are other reasons similar to the first one. miraculously, we intend to overcome the problem of instantiating objects (Row 1 ). once we assign the value returned by CreateObject to global_object, we still need to hit the same problem-global_object will become a copy of the returned 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 go that far-once the CreateObject returns $ obj, we will destroy the reference (row 34th). This is the third reason.
So how can we correct this? There are two options. first, add the & Symbol in all places, as in example 6.22 (rows 24th, 28, 31, 37 ). II. if you are lucky enough to use PHP5, you can forget all of the above, PHP5 will automatically consider this for you. if you want to know how PHP5 considers these issues, read on.
Listing 6.22 WTMA syndrome in PHP 4 WTMA syndrome in PHP4Copy codeThe Code is as follows: 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 return $ 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 "Value is". $ global_obj-> getValue (). "\ n ";
41 print "Value is". $ global_obj-> getValueFromMe (). "\ n ";

PHP5 is the first PHP version that regards objects 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 transmitted by reference, while other types of data (such as integer, string, array) all are passed through values. most importantly, there is no need to use the & symbol to indicate that objects are passed through references.
Object-Oriented Programming extensively utilizes the complex relationships between object networks and objects, all of which need to be referenced. in earlier versions of PHP, you must explicitly specify the reference. therefore, objects are now moved by reference by default, and the object is copied only when it is explicitly required to be copied, which is better than before.
How is it implemented?
Before PHP5, all values exist in a special structure named 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 the value is passed to the function or returned from the function, these values are copied and a structure with the same content is created at another address in the memory.
In PHP5, values are still stored in the zval structure, except for objects. an Object exists in a structure called Object Store, and each Object has a different ID. in Zval, the object pointer is stored instead of the object itself. when copying a zval structure that holds an object, for example, passing an object as a parameter to a function, we will not copy any data. we only keep the same Object pointer and the other zval notifies the Object Store to which this specific Object points. because the Object itself is located in the Object Store, any changes we make to it will affect all the zval structures that hold the Object pointer. the indirect effects of this append make PHP objects look like they are always passed by reference, in a transparent and efficient way.
With PHP5, we can now go back to example 6.21. Except for all the & symbols, all codes can still work normally. when we hold a reference in the constructor (row 4th), no & symbol is needed.

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.