PHP4 true OO1
The author of this article Johan Persson is famous in PHP
JPGraph
The developer of the chart class library. This article is a summary of several small problems that need to pay attention to when conducting object-oriented development in PHP4.
Translation: Binzy Wu [Mail: Binzy At Justdn Dot COM], limited horizontal, welcome to discuss. 2004-2-4
Original URL: http://www.aditus.nu/jpgraph/jpg_phpoo.php
Introduction
The objects of this article are those who have used more mature OO languages, such as Eiffel, Java, C # 2 or C (), have developed. With a lot of Semantics 3 when using PHP4 for complete OO development (semantic)
The trap 4.
The content of Xiuben is helpful to avoid my fault.
Quote VS copy semantics
This is basically wrong (at least for me). Even in the PHP document, you can read more use of PHP4 more copies (such as the object-oriented language I know), but this is still Will make you finally troubled at some of you.
The next two parts are used to illustrate two small examples, and copy semanties in these two examples may be surprised.
It is important to keep in mind that the variable of a class is not a pointer to the class, but the actual class itself 5. Most problems have triggered the misunderstanding of the assignment operator (=), that is, it is an alias for an object, and In fact, it is a new copy. For example, $ Myobj is an instance of a class, and it has a set () method. The following code may not work like a C (or Java) programmer .
Function Somefunction ($ AOBJ) {$ AOBJ-> Set (10);
...
Somefunction ($ myObj);
...
So now, it is easy to think that the set () method called by this function acts on $ MyObj. But this is wrong!
In fact, $ myobj is copied as a new, the same copy as the original object ---- Parameter $ AOBJ. Then when the set () method is called, it only works on local copy and non-original parameters --- $ myobj.
Various above problems occur in places that contain direct or indirect (such as above) assignment.
For functions, you can act as what you expect (maybe), then you have to tell PHP to pass objects by modifying methods, such as:
Function Somefunction (& $ AOBJ)
If you try to try the code above again, then you will find that the set () method will act on the original parameters, because now we create a $ myobj alias --- $ AOBJ in the role.
But you have to be careful, because even the & operator is not to save you at any time, as will be exemplified below.
Get reference from a reference
Assume that there is the following code:
$ myObject = new someclass (); $ myReftoobject = & $ myObject;
If we want a reference to a copy (for some reason), what should we do? You may have a reference to the $ myReftoBject is already quoted:
$ mycopyreftoobject = $ myReftoObject;
Correct? No! PHP will create a new copy of the object referenced to myReftoObject. If you want to copy an object's reference, you have to write:
$ MyCopyReftoObject = & $ MyReftoObject;
In an example of the C of the example as previously described, a reference is created. It is different from it in PHP. This is an experienced c programmer's intuition assignment, and this will be yours. Source of small bug in the PHP program.
CAUTION Carefully thereby generated indirect (transfer parameters) or direct problems.
I personally reached the conclusion that the best way to avoid these semantic traps is to always pass objects or object assignments. This not only improves running speed (less data copy), but also like me The old dog is more predictable by words.
Use references to $ this in the constructor
Initializing the object of an object is a common mode as an object of other object discoverers (OBServer6). The following line is an example: Class Bettery
{
Function Bettery () {...};
Function Addobserver ($ Method, & $ OBJ)
{
$ this-> obs [] = array ($ OBJ, & $ MethOD)
}
Function notify () {...}
}
Class Display
{
Function Display (& $ BATT)
{
$ BATT-> Addobserver ("Batterynotify", $ this);
}
Function BatteryNotify () {...}
}
However, this will not work properly, if you are so instantiated:
$ mybattery = new battery (); $ myDisplay = new display ($ mybattery); this error is in the constructor in the constructor when New is in New WEs. Instead, it will return a copy of the most recently created object. That is, the object transmitted when calling addObserver () is not the same. Then when the Battery class attempts to inform all its observer (by calling their Notify methods), it does not call we created The Display class is a class representing the $ THIS (that is, the copy of the Display class we created). So if the Notify () method updates some instance variables, not as the original Display class will be updated because the update In fact, it is a copy.
To make it work, you must return the constructor to the same object, just as symbolized with the original $ this. You can construct to DISPLAY, such as $ myDisplay = & new display ($ mybattery); a direct The result is that any Display class's Client must understand the DISPLAY implementation details. In fact, this will generate a problem that may cause debate: All objects must be used to use additional & symbols. I am basically safe, but Ignore that it may get unwanted examples as described above at some point.
Another method is used in JPGRAPH. That is, you need to use the so-called second-phase construct of "init ()" method to "new" by adding a secure usage & $ this reference (just because of the construction The $ this reference in the function is not as expected as a copy of the object). Therefore, the above example will be implemented as follows:
$ mybattery = new battery ();
$ myDisplay = new display ();
$ myDisplay-> init ($ mybattery);
Such as "Linearscale" class in jpgraph.php.
Use foreach
Another problem with different results is the problem of "Foreach" structure. Study different versions of the following two cyclic structures.
// Version 1Foreach ($ this-> Plots as $ P) {$ P-> Update ();} ... // version 2for ($ I = 0; $ i
It is now a $ 10 issue 7: Version1 == VERSION2?
Surprising answer is: no! This is fine but is a key difference. In Version 1, the update () method will act on the "Plots []" array of objects. Therefore, the original object in the array will not Updated.
The Update () method in Version 2 will be as expected to act on the object in the "Plots []" array.
As the first part is stated, this is the result of PHP to process the object instance as the object itself rather than as an object reference.
Translation:
1. OO: Object-Oriented, object-oriented .2. Originally there is no C #, all because of Binzy personal hobby.
3. Semantic is translated into "Semantics" herein, if any suggestions are contacted, please contact Binzy.
4. There is a famous "C Gotchas" in C .
5. The class here should refer to instance, ie instances.
6. See "[gof95]", "Design Patterns".
7. Have a very interesting little story about the transaction:
Some people bought a horse with $ 70, and sold out with a price of $ 70; then he bought it again with $ 80, and finally sold it with a price of $ 90. In this trading of this horse, he ? (A) lost $ 10; (b) balance of payments; (c) earned $ 10; (d) earned $ 20; (e) earned $ 30.
This is a simple arithmetic question for Mel and Berk, a psychologist, US Michigan University. The conditions are very clear, this is a two trading, earning $ 10 every time, but many people think that when he bought it with $ 80, he has lost $ 10. Interestingly, the same problem, One way to say: There is a person who bought a white horse with $ 60, and sold it with a value of 70 yuan; then bought a black horse with $ 90, and sold it with a value of $ 90. In this In the trading of the Piles, he ____ (listed the same five choices). At this time, another group of college students will answer the question when answering the above questions.