Section 15th -- Development of the Zend Engine -- ClassesandObjectsinPHP5 [15] _ PHP Tutorial

Source: Internet
Author: User
Section 15th -- Development of the Zend Engine -- ClassesandObjectsinPHP5 [15]. 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, particularly the differences between it and the models in the previous versions of PHP. when the summer of 1997 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
 value;        }    }    $obj = new Example();    $obj->PrintValue(); ?> 



Structured programming in Listing 6.19 PHP 3 structural programming PHP3 PHP3
  


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 PHP4
  



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 PHP4
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() . ""; 41        print "Value is " . $global_obj->getValueFromMe() . ""; 




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 PHP4
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() . ""; 41        print "Value is " . $global_obj->getValueFromMe() . ""; 



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.
<

Copyright 15th --zendengine development in the last section of this chapter, Zeev discusses the object model brought about by the Zend Engine, particularly the differences between it and the model in the previous versions of PHP. when the summer of 1997...

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.