Section 15th--zend Engine development--Classes and Objects in PHP5 [15]

Source: Internet
Author: User
Tags array arrays functions object model version versions access zend
OBJECT|PHP5/*
+-------------------------------------------------------------------------------+
| = This article is for Haohappy read <<core PHP programming>>
| = Notes from the chapter classes and objects
| = translation-oriented + personal experience
| = Please do not reprint to avoid any unnecessary trouble that may occur, thank you
| = Welcome to criticize, hope and all PHP enthusiasts to progress together!
+-------------------------------------------------------------------------------+
*/

The 15th section--zend engine development

In the last section of this chapter, Zeev discusses the object model brought by the Zend engine, specifically how it differs from the models in the previous versions of PHP.
When we developed the PHP3 in the summer of 1997, we had no plans to make PHP have object-oriented capabilities. There were no ideas about classes and objects at the time. PHP3 is a purely process-oriented language. However, support for classes was added to the PHP3 Alpha version in the evening of 1997.8.27. Adding a new feature to PHP was just a little discussion, because there were too few people to explore PHP at that time. Since August 1997, PHP has taken the first step toward object-oriented programming languages.

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 just a cool way to access arrays. Instead of using $foo["bar", you can use a $foo->bar that looks more beautiful. The main advantage of object-oriented methods is to store functionality through member functions or methods. Example 6.18 shows a typical block of code. But it's not much different from the one in example 6.19.

Object-oriented programming in Listing 6.18 PHP 3 object-oriented programming PHP3
<?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 PHP3
<?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);
?>


We'll write two lines of code in the class, or we can pass the array to the function. But given that there is no difference between the two options in PHP3, we can still access the array only as a "syntactic 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 in a wall. Luckily, there weren't many people who wanted to use PHP for object-oriented development at the time (PHP3).

PHP4 changed the situation. The new version brings the concept of a reference (reference), which allows different identifiers for 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

<?php
$a = 5;

$b points to the same place in memory as $a $b and $a point to the same address in memory
$b = & $a;

We ' re changing $b, since $a is pointing to change $b, point to address change
The same place-it changes too the address that the $a points to also changes
$b = 7;

Prints 7 output 7
Print $a;
?>



Since building a network of objects pointing to each other is the foundation of all object-oriented design patterns, this improvement is of great significance. When references allow for more powerful object-oriented applications, The same way that PHP treats objects and other types of data is extremely painful to developers. As any PHP4 programmer will tell you, the application will encounter Wtma (Way Too Many ampersands too much &) syndrome. If you want to build a practical application, you will feel extremely painful, see example 6.21 you understand.

Problems with objects in the 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 {
$this->value = $val;
11}
12
function GetValue ()
14 {
return $this->value;
16}
17
The function Getvaluefromme ()
19 {
$this->me->value;
21}
22}
23
function CreateObject ($class _type)
25 {
Switch ($class _type) {
Case "Foo":
$obj = new Myfoo ();
break;
Case "Bar":
$obj = new Mybar ();
break;
33}
return $obj;
35}
36
Panax Notoginseng $global _obj = CreateObject ("foo");
$global _obj->setvalue (7);
39
Print "Value is". $global _obj->getvalue (). "N";
The 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 the
We have three other member functions: one setting This->value value, one returning This->value value, and the other returning This->value->me value. But--$this not the same thing? Is the value returned by Myfoo::getvalue () and Myfoo::getvaluefromme () not the same?

First, we call CreateObject ("Foo"), which returns an object of Myfoo type. Then we call Myfoo::setvalue (7). Finally, we call Myfoo::getvalue () and Myfoo::getvaluefromme () and expect to get a return value of 7.
Of course, if we get 7 in any case, the above example will not be the most meaningless example in this book. So I'm sure you've guessed--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. There are three good reasons why —--.

First, look at the constructor. When inside 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 within the constructor. When the constructor ends, PHP reconstructs the object (the result of new Myfoo, line 28th) to $obj. Because the object is not treated as a specialization, as with any other data type, assigning x to Y means Y is a copy of X. In other words, obj will be a copy of the new Myfoo, and the 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 of the other 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 (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 no longer be the same. That's the second reason.

But, in fact, we can't go that far-once CreateObject returns to $obj, we will 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, just like example 6.22 (24th, 28, 31, 37 lines). Two. If you are lucky 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.

PHP4 syndrome of Listing 6.22 Wtma syndrome in PHP 4 WTMA
1 class Myfoo {
2 function Myfoo ()
3 {
4 $this->me = & $this;
5 $this->value = 2;
6}
7
8 function SetValue ($val)
9 {
$this->value = $val;
11}
12
function GetValue ()
14 {
return $this->value;
16}
17
The function Getvaluefromme ()
19 {
$this->me->value;
21}
22};
23
function &createobject ($class _type)
25 {
Switch ($class _type) {
Case "Foo":
$obj =& new Myfoo ();
break;
Case "Bar":
$obj =& New Mybar ();
break;
33}
return $obj;
35}
36
Panax Notoginseng $global _obj =& CreateObject ("foo");
$global _obj->setvalue (7);
39
Print "Value is". $global _obj->getvalue (). "N";
The print "Value is". $global _obj->getvaluefromme (). "N";



PHP5 is the first PHP version to look at objects differently from other types of data. From a user's point of view, this proves its very clear way-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 & symbol to convey an object by reference.

Object-oriented programming extensively utilizes the complex relationships between object networks and objects, all of which require references. In previous versions of PHP, you need to indicate the reference. Therefore, it is better to move objects by default with references and to replicate objects only if you explicitly require replication.

How is it achieved?

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, the values are replicated 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 the object's pointer is stored. When copying a zval structure that holds objects, such as we pass an object to a function as an argument, we no longer replicate any data. We just keep the same object pointer and notify another zval that this particular object is now pointing to the object Store. Because the object itself is located in the object Store, any changes we make to it will affect all zval constructs that hold the object's pointer. 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, removing all the & symbols, and all the code will still work. A & symbol is not used when we hold a reference in the constructor (line 4th).


< completion >

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.