Section 5-clone-ClassesandObjectsinPHP5 [5]. Section 5-clone an object model in PHP5 to call an object through reference, but sometimes you may want to create a copy of the object and expect that the change of the original object will not affect the copy. in this case, section 5-clone
The object model in PHP5 calls an object through reference, but sometimes you may want to create a copy of the object and expect that the change of the original object will not affect the copy. for this purpose, PHP defines a special method called _ clone. like _ construct and _ destruct, the front has two underscores.
By default, the _ clone method is used to create an object with the same attributes and methods as the original object. if you want to change the default content during cloning, overwrite (attribute or method) in _ clone ).
The clone method can have no parameters, but it also contains the this and that pointers (that points to the copied object ). if you choose to clone yourself, you must be careful to copy any information contained in your object, from that to this. if you use _ clone to copy. PHP will not perform any implicit replication,
The following shows an example of automatic objects using sequence numbers:
Green c hinaip ower. com7AT0x
Class ObjectTracker // object tracker
{
Private static $ nextSerial = 0;
Private $ id;
Private $ name;
Function _ construct ($ name) // Constructor
{
$ This-> name = $ name;
$ This-> id = + self: $ nextSerial;
}
Function _ clone () // clone
{
$ This-> name = "Clone of $ that-> name ";
$ This-> id = + self: $ nextSerial;
}
Function getId () // Obtain the id attribute value
{
Return ($ this-> id );
}
The object model in PHP5 calls an object through reference, but sometimes you may want to create a copy of the object and expect that the change of the original object will not affect the copy. so...