


Section 15--The Development of Zend Engine--ClassesandObjectsinPHP515_PHP Tutorial
/* +-------------------------------------------------- --------------------------------+ | = This article is read by Haohappy> | = Classes and Objects 1 Chapter's notes | = Translation + personal experience | = To avoid unnecessary trouble, please do not reprint, thank you | = Criticisms and corrections are welcome, and I hope to make progress together with all PHP enthusiasts! +------- -------------------------------------------------- -----------------------+ */ Section 15--The Development of Zend Engine In the last section of this chapter, Zeev discusses the objects brought by Zend Engine model, specifically mentioning how it differed from the models in previous versions of PHP. When we developed PHP 3 in the summer of 1997, we had no plans to make PHP object-oriented. There was nothing to do with classes and objects. The idea. PHP3 is a purely process-oriented language. However, support for classes was added in the PHP3 alpha version on the evening of August 27, 1997. Adding a new feature to PHP only required minimal discussion at that time, because PHP was explored at that time There are too few people. So from August 1997, PHP took the first step towards an object-oriented programming language. Indeed, this is only the first step. Because there are very few relevant ideas in this design, for objects The support 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 the prettier $foo->bar. Object-oriented approach The main advantage is to store functionality through member functions or methods. A typical code block is shown in Example 6.18. But it is not really that different from the approach in Example 6.19. Listing 6.18 PHP 3 object-oriented programming in PHP3 Object-oriented programming value; } } $obj = new Example(); $obj->PrintValue(); ?> Listing 6.19 PHP 3 structural programming PHP3 Structured programming in PHP3 Above we write in the class The above two lines of code, or explicitly passing the array to the function. But considering that there is no difference between these two options in PHP3, we can still access the array just by using the object model as a "syntactic whitewash". Want to use People who used PHP for object-oriented development, especially those who wanted to use design patterns, quickly found that they hit a wall. Fortunately, at the time (PHP3 era), not many people wanted to use PHP for object-oriented development. PHP4 changed This is the case. The new version brings the concept of reference, which allows different identifiers in PHP to point to the same address in memory. This means that you can use two or more names for the same variable Naming, as in Example 6.20. Listing 6.20 PHP 4 references References in PHP4 Since building a network of objects that point to each other is the basis of all object-oriented design patterns, this improvement is of great significance. When references allow the creation of more As powerful as object-oriented applications are, PHP treats objects the same as other types of data, causing developers great pain. 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. Take a look at Example 6.21 and you will understand. Listing 6.21 Problems with objects in PHP 4 Problems with objects in PHP 4 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 up that we have three other member functions : One sets the value of this->value; one returns the value of this->value; the other returns the value of this->value->me. But isn't --$this the same thing? MyFoo::getValue() Isn't it the same as the value returned by MyFoo::getValueFromMe()? 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(), expect a return value of 7. Of course, if we got 7 in every case, the above example wouldn't be the least meaningful example in the book. So I trust you Already guessed it - we won't get two 7s. But what will we get, and more importantly, why? The results we will get are 7 and 5. As for why - three would be nice The reason. 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 inside the constructor. When the constructor ends, PHP has to re-create the object (the result of new MyFoo, line 28) and assign it to $obj. Because objects are not treated specially, just like any other data type, assigning X to Y means that Y is X. A copy. That is, 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 unchanged. Object—this. Voila-obj and obj->me are no longer the same thing—change one and the other remains unchanged. That’s the first reason. There are other reasons similar to the first. Miraculously we intend to overcome the problem of instantiating objects (line 28). Once we assign the value returned by CreateObject to global_object, we still run into 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 reason number two. But, actually we can't go that far yet — once CreateObject returns $obj, we'll destroy the reference (line 34). That's reason number three. So , how do we correct this? There are two options. One is to add ampersands everywhere, like in Example 6.22 (lines 24, 28, 31, 37). 2. If you are lucky enough to use PHP5, you can Forget all the above, PHP5 will automatically take care of this for you. If you want to know how PHP5 takes this into account, keep reading. 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() . "n"; 41 print "Value is " . $global_obj->getValueFromMe() . "n"; PHP5 is the first PHP version to treat objects as different from other types of data. From a user's perspective, this proves to be very clear - in PHP5, objects are always is 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 indicate passing objects by reference. Object-oriented programming widely utilizes Object networks and complex relationships between objects require the use of references. In previous versions of PHP, references needed to be explicitly specified. Therefore, objects are now moved by reference by default, and objects are copied only when explicitly requested to do so. , which is better than before. How is it implemented? Before PHP5, all values were stored 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 values are passed to or returned from functions, these values are copied, creating a structure with the same content at another address in memory. In PHP5, the value is still stored as a zval In the structure, except for objects. Objects exist in a structure called Object Store, and each object has a different ID. In Zval, the object itself is not stored, but the pointer of the object is stored. When copying a holding object zval structure, for example, if we pass an object as a parameter to a function, we no longer copy any data. We just keep the same object pointer and let another zval notify the Object Store that this specific object currently points to. Because the object itself is located in Object Store, any changes we make to it will affect all zval structures holding pointers to that object. This additional indirection makes it appear as if PHP objects are always passed by reference, in a transparent and efficient way . Using PHP5, we can now go back to Example 6.21 and remove all the ampersands, and everything will still work fine. Not a single ampersand is used when we hold a reference in the constructor (line 4).

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to set up keyboard startup on Gigabyte's motherboard. First, if it needs to support keyboard startup, it must be a PS2 keyboard! ! The setting steps are as follows: Step 1: Press Del or F2 to enter the BIOS after booting, and go to the Advanced (Advanced) mode of the BIOS. Ordinary motherboards enter the EZ (Easy) mode of the motherboard by default. You need to press F7 to switch to the Advanced mode. ROG series motherboards enter the BIOS by default. Advanced mode (we use Simplified Chinese to demonstrate) Step 2: Select to - [Advanced] - [Advanced Power Management (APM)] Step 3: Find the option [Wake up by PS2 keyboard] Step 4: This option The default is Disabled. After pulling down, you can see three different setting options, namely press [space bar] to turn on the computer, press group

1. Processor When choosing a computer configuration, the processor is one of the most important components. For playing games like CS, the performance of the processor directly affects the smoothness and response speed of the game. It is recommended to choose Intel Core i5 or i7 series processors because they have powerful multi-core processing capabilities and high frequencies, and can easily cope with the high requirements of CS. 2. Graphics card Graphics card is one of the important factors in game performance. For shooting games such as CS, the performance of the graphics card directly affects the clarity and smoothness of the game screen. It is recommended to choose NVIDIA GeForce GTX series or AMD Radeon RX series graphics cards. They have excellent graphics processing capabilities and high frame rate output, and can provide a better gaming experience. 3. Memory power

SPDIFOUT connection line sequence on the motherboard. Recently, I encountered a problem regarding the wiring sequence of the wires. I checked online. Some information says that 1, 2, and 4 correspond to out, +5V, and ground; while other information says that 1, 2, and 4 correspond to out, ground, and +5V. The best way is to check your motherboard manual. If you can't find the manual, you can use a multimeter to measure it. Find the ground first, then you can determine the order of the rest of the wiring. How to connect motherboard VDG wiring When connecting the VDG wiring of the motherboard, you need to plug one end of the VGA cable into the VGA interface of the monitor and the other end into the VGA interface of the computer's graphics card. Please be careful not to plug it into the motherboard's VGA port. Once connected, you can

Glodon Software is a software company focusing on the field of building informatization. Its products are widely used in all aspects of architectural design, construction, and operation. Due to the complex functions and large data volume of Glodon software, it requires high computer configuration. This article will elaborate on the computer configuration recommendations of Glodon Software from many aspects to help readers choose a suitable computer configuration processor. Glodon Software requires a large amount of data calculation and processing when performing architectural design, simulation and other operations. Therefore, the requirements for the processor are higher. It is recommended to choose a multi-core, high-frequency processor, such as Intel i7 series or AMD Ryzen series. These processors have strong computing power and multi-thread processing capabilities, and can better meet the needs of Glodon software. Memory Memory is affecting computing

Which version of the graphics card driver is best to use? 1. There is no absolute best version. It is most important to choose the version that suits your computer; 2. Because the applicability and stability of the graphics card driver version are related to the computer hardware environment and system configuration; 3. You can check the detailed information of the computer and graphics card on the official website, select the appropriate driver version based on the information, or refer to the reviews of other users. It is recommended to back up the system before installing the driver to avoid unexpected situations. Graphics card driver version 472.19 series is an excellent choice. Currently, the driver compatibility of version 472 is the best. Installing version 472 of the driver can also maximize the performance of the graphics card. The NVIDIA graphics card driver Win7 installation version, numbered 2, 472.19, is a product with remarkable quality.

Redmi officially announced today that the new Redmi GPro 2024 will be officially released on March 4. In other words, next week we will have the release of this exciting new product. RedmiGPro2024 makes its full debut as an e-sports flagship, deeply integrating the mobile phone industry capabilities into the notebook business, presenting 210W super performance release, and Redmi performance reaching a new high. Equipped with i9-14900HX processor and RTX4060 graphics card, it perfectly combines e-sports and creation to achieve dual evolution. From this point of view, the performance of this new product will be improved again, and the actual effect is exciting. It was mentioned in the official warm-up that the new Redmi G Pro 2024 brings the PC version of the violent engine. Mobile phone technology empowerment, three major factors lead

The intelligent NPC created by Academician Huang in "Cyberpunk 2077" can already speak Chinese? Qubit's first-hand experience, witnessing NPCs conversing fluently in both Chinese and English, with natural expressions and movements, and matching mouth shapes... If there wasn't a screen in front of me, it would really feel like being there. . At this year's CES exhibition, Nvidia used its intelligent engine Avatar Cloud Engine (ACE) to make game NPCs "alive", which caused quite a shock. △The intelligent NPC displayed at CES uses ACE. The characters in the game can have realistic voice conversations with players, while showing vivid expressions and body movements without having to prepare a script in advance. At the time of its debut, there were Ubisoft, Tencent, NetEase, MiHoYo and other countries.

I am planning to go backpacking in Tibet. ① How many liters of bag should I carry? Please tell me what you think is the best configuration. I am 170 and have good physical strength. The first time I went hiking, the amount was 60 liters or more. The amount of hiking was less than 60 liters. The entire journey was by car. You don’t need a backpack, a suitcase is more convenient. If you really need to carry something with you, a 25-40 liter bag is more than enough. Necessary supplies for Tibet travel: sunglasses, sun hat, sunscreen, skin cream, lip balm, long-sleeved top, Sweater; for special travel or travel to Ali, northern Tibet, and Sichuan-Tibet line, it is recommended to bring: sleeping bag (cold protection), sheets (dirty protection), down jacket, travel shoes or hiking shoes, slippers, toothbrush, toothpaste, towel, rolling paper , paper underwear, disinfectant wipes, flashlight, waterproof matches, knives, rope. Can a computer be carried in the front bag? Can a computer be carried in the front bag? Some backpacks have it.
