Hướng dẫn explain cloning object in php - giải thích đối tượng nhân bản trong php

Tạo một bản sao của một đối tượng với các thuộc tính được sao chép hoàn toàn không phải lúc nào cũng là hành vi mong muốn. Một ví dụ điển hình về nhu cầu về các hàm tạo sao Có đối tượng mới giữ tài nguyên của cửa sổ mới. Một ví dụ khác là nếu đối tượng của bạn giữ một tham chiếu đến một đối tượng khác mà nó sử dụng và khi bạn sao chép đối tượng cha, bạn muốn tạo một thể hiện mới của đối tượng khác này để bản sao có bản sao riêng.

Một bản sao đối tượng được tạo bằng cách sử dụng từ khóa clone [gọi phương thức __clone [] của đối tượng nếu có thể].

$copy_of_object = clone $object;

Khi một đối tượng được nhân bản, PHP sẽ thực hiện một bản sao nông của tất cả các thuộc tính của đối tượng. Bất kỳ thuộc tính nào là tài liệu tham khảo cho các biến khác sẽ vẫn là tài liệu tham khảo.

__clone []: Void[]: void

Sau khi nhân bản hoàn tất, nếu phương thức __clone [] được xác định, thì phương thức __clone [] của đối tượng mới được tạo sẽ được gọi, để cho phép bất kỳ thuộc tính cần thiết nào cần thay đổi.

Ví dụ #1 nhân bản một đối tượng

Ví dụ trên sẽ xuất ra:

Original Object:
MyCloneable Object
[
    [object1] => SubObject Object
        [
            [instance] => 1
        ]

    [object2] => SubObject Object
        [
            [instance] => 2
        ]

]
Cloned Object:
MyCloneable Object
[
    [object1] => SubObject Object
        [
            [instance] => 3
        ]

    [object2] => SubObject Object
        [
            [instance] => 2
        ]

]

Có thể truy cập vào một thành viên của một đối tượng mới được nhân bản trong một biểu thức duy nhất:

Ví dụ #2 Thành viên truy cập của đối tượng mới nhân bản

Ví dụ trên sẽ xuất ra một cái gì đó tương tự như:

Jorge Dot Villalobos tại Gmail Dot Com ¶

17 năm trước

I think it's relevant to note that __clone is NOT an override. As the example shows, the normal cloning process always occurs, and it's the responsibility of the __clone method to "mend" any "wrong" action performed by it.

jojor tại gmx dot net ¶

12 năm trước

Here is test script i wrote to test the behaviour of clone when i have arrays with primitive values in my class - as an additonal test of the note below by jeffrey at whinger dot nl


0

14 năm trước

Stanislav Dot Eckert tại Vizson Dot de ¶

__construct[] {
        
$this->instance = ++self::$instances;
    }

    public function

__clone[] {
        
$this->instance = ++self::$instances;
    }
}

class

MyCloneable
{
    public 
$object1;
    public 
$object2;

    function

__clone[]
    {
        
// Force a copy of this->object, otherwise
        // it will point to same object.
        
$this->object1 = clone $this->object1;
    }
}
$obj = new MyCloneable[];$obj->object1 = new SubObject[];
$obj->object2 = new SubObject[];$obj2 = clone $obj;

print[

"Original Object:\n"];
print_r[$obj];

print[

"Cloned Object:\n"];
print_r[$obj2];?>
1

__construct[] {
        
$this->instance = ++self::$instances;
    }

    public function

__clone[] {
        
$this->instance = ++self::$instances;
    }
}

class

MyCloneable
{
    public 
$object1;
    public 
$object2;

    function

__clone[]
    {
        
// Force a copy of this->object, otherwise
        // it will point to same object.
        
$this->object1 = clone $this->object1;
    }
}
$obj = new MyCloneable[];$obj->object1 = new SubObject[];
$obj->object2 = new SubObject[];$obj2 = clone $obj;

print[

"Original Object:\n"];
print_r[$obj];

print[

"Cloned Object:\n"];
print_r[$obj2];?>
2

__construct[] {
        
$this->instance = ++self::$instances;
    }

    public function

__clone[] {
        
$this->instance = ++self::$instances;
    }
}

class

MyCloneable
{
    public 
$object1;
    public 
$object2;

    function

__clone[]
    {
        
// Force a copy of this->object, otherwise
        // it will point to same object.
        
$this->object1 = clone $this->object1;
    }
}
$obj = new MyCloneable[];$obj->object1 = new SubObject[];
$obj->object2 = new SubObject[];$obj2 = clone $obj;

print[

"Original Object:\n"];
print_r[$obj];

print[

"Cloned Object:\n"];
print_r[$obj2];?>
3

7 năm trước

Hayley Watson ¶

__construct[] {
        
$this->instance = ++self::$instances;
    }

    public function

__clone[] {
        
$this->instance = ++self::$instances;
    }
}

class

MyCloneable
{
    public 
$object1;
    public 
$object2;

    function

__clone[]
    {
        
// Force a copy of this->object, otherwise
        // it will point to same object.
        
$this->object1 = clone $this->object1;
    }
}
$obj = new MyCloneable[];$obj->object1 = new SubObject[];
$obj->object2 = new SubObject[];$obj2 = clone $obj;

print[

"Original Object:\n"];
print_r[$obj];

print[

"Cloned Object:\n"];
print_r[$obj2];?>
4

__construct[] {
        
$this->instance = ++self::$instances;
    }

    public function

__clone[] {
        
$this->instance = ++self::$instances;
    }
}

class

MyCloneable
{
    public 
$object1;
    public 
$object2;

    function

__clone[]
    {
        
// Force a copy of this->object, otherwise
        // it will point to same object.
        
$this->object1 = clone $this->object1;
    }
}
$obj = new MyCloneable[];$obj->object1 = new SubObject[];
$obj->object2 = new SubObject[];$obj2 = clone $obj;

print[

"Original Object:\n"];
print_r[$obj];

print[

"Cloned Object:\n"];
print_r[$obj2];?>
5

__construct[] {
        
$this->instance = ++self::$instances;
    }

    public function

__clone[] {
        
$this->instance = ++self::$instances;
    }
}

class

MyCloneable
{
    public 
$object1;
    public 
$object2;

    function

__clone[]
    {
        
// Force a copy of this->object, otherwise
        // it will point to same object.
        
$this->object1 = clone $this->object1;
    }
}
$obj = new MyCloneable[];$obj->object1 = new SubObject[];
$obj->object2 = new SubObject[];$obj2 = clone $obj;

print[

"Original Object:\n"];
print_r[$obj];

print[

"Cloned Object:\n"];
print_r[$obj2];?>
6

__construct[] {
        
$this->instance = ++self::$instances;
    }

    public function

__clone[] {
        
$this->instance = ++self::$instances;
    }
}

class

MyCloneable
{
    public 
$object1;
    public 
$object2;

    function

__clone[]
    {
        
// Force a copy of this->object, otherwise
        // it will point to same object.
        
$this->object1 = clone $this->object1;
    }
}
$obj = new MyCloneable[];$obj->object1 = new SubObject[];
$obj->object2 = new SubObject[];$obj2 = clone $obj;

print[

"Original Object:\n"];
print_r[$obj];

print[

"Cloned Object:\n"];
print_r[$obj2];?>
7

__construct[] {
        
$this->instance = ++self::$instances;
    }

    public function

__clone[] {
        
$this->instance = ++self::$instances;
    }
}

class

MyCloneable
{
    public 
$object1;
    public 
$object2;

    function

__clone[]
    {
        
// Force a copy of this->object, otherwise
        // it will point to same object.
        
$this->object1 = clone $this->object1;
    }
}
$obj = new MyCloneable[];$obj->object1 = new SubObject[];
$obj->object2 = new SubObject[];$obj2 = clone $obj;

print[

"Original Object:\n"];
print_r[$obj];

print[

"Cloned Object:\n"];
print_r[$obj2];?>
8

__construct[] {
        
$this->instance = ++self::$instances;
    }

    public function

__clone[] {
        
$this->instance = ++self::$instances;
    }
}

class

MyCloneable
{
    public 
$object1;
    public 
$object2;

    function

__clone[]
    {
        
// Force a copy of this->object, otherwise
        // it will point to same object.
        
$this->object1 = clone $this->object1;
    }
}
$obj = new MyCloneable[];$obj->object1 = new SubObject[];
$obj->object2 = new SubObject[];$obj2 = clone $obj;

print[

"Original Object:\n"];
print_r[$obj];

print[

"Cloned Object:\n"];
print_r[$obj2];?>
9

0

1

2

3

4

5

6

7

8

9

I think it's relevant to note that __clone is NOT an override. As the example shows, the normal cloning process always occurs, and it's the responsibility of the __clone method to "mend" any "wrong" action performed by it. 0

9

I think it's relevant to note that __clone is NOT an override. As the example shows, the normal cloning process always occurs, and it's the responsibility of the __clone method to "mend" any "wrong" action performed by it. 2

Original Object:
MyCloneable Object
[
    [object1] => SubObject Object
        [
            [instance] => 1
        ]

    [object2] => SubObject Object
        [
            [instance] => 2
        ]

]
Cloned Object:
MyCloneable Object
[
    [object1] => SubObject Object
        [
            [instance] => 3
        ]

    [object2] => SubObject Object
        [
            [instance] => 2
        ]

]
5

14 năm trước

3 năm trước

I think it's relevant to note that __clone is NOT an override. As the example shows, the normal cloning process always occurs, and it's the responsibility of the __clone method to "mend" any "wrong" action performed by it. 4

Emile tại Webflow Dot NL ¶

15 năm trước

I think it's relevant to note that __clone is NOT an override. As the example shows, the normal cloning process always occurs, and it's the responsibility of the __clone method to "mend" any "wrong" action performed by it. 5

I think it's relevant to note that __clone is NOT an override. As the example shows, the normal cloning process always occurs, and it's the responsibility of the __clone method to "mend" any "wrong" action performed by it. 6

I think it's relevant to note that __clone is NOT an override. As the example shows, the normal cloning process always occurs, and it's the responsibility of the __clone method to "mend" any "wrong" action performed by it. 7

I think it's relevant to note that __clone is NOT an override. As the example shows, the normal cloning process always occurs, and it's the responsibility of the __clone method to "mend" any "wrong" action performed by it. 8

Original Object:
MyCloneable Object
[
    [object1] => SubObject Object
        [
            [instance] => 1
        ]

    [object2] => SubObject Object
        [
            [instance] => 2
        ]

]
Cloned Object:
MyCloneable Object
[
    [object1] => SubObject Object
        [
            [instance] => 3
        ]

    [object2] => SubObject Object
        [
            [instance] => 2
        ]

]
5

Tolgakaragol tại Gmail Dot Com ¶

Ben ở cuối cùng chấm fm ¶

Here is test script i wrote to test the behaviour of clone when i have arrays with primitive values in my class - as an additonal test of the note below by jeffrey at whinger dot nl0

Here is test script i wrote to test the behaviour of clone when i have arrays with primitive values in my class - as an additonal test of the note below by jeffrey at whinger dot nl1

Original Object:
MyCloneable Object
[
    [object1] => SubObject Object
        [
            [instance] => 1
        ]

    [object2] => SubObject Object
        [
            [instance] => 2
        ]

]
Cloned Object:
MyCloneable Object
[
    [object1] => SubObject Object
        [
            [instance] => 3
        ]

    [object2] => SubObject Object
        [
            [instance] => 2
        ]

]
5

Bản sao đối tượng có nghĩa là gì giải thích nó bằng một ví dụ?

Nhân bản đối tượng đề cập đến việc tạo ra một bản sao chính xác của một đối tượng. Nó tạo ra một thể hiện mới của lớp của đối tượng hiện tại và khởi tạo tất cả các trường của nó với chính xác nội dung của các trường tương ứng của đối tượng này. Sử dụng toán tử gán để tạo một bản sao của biến tham chiếu.the creation of an exact copy of an object. It creates a new instance of the class of the current object and initializes all its fields with exactly the contents of the corresponding fields of this object. Using Assignment Operator to create a copy of the reference variable.

Php có hỗ trợ nhân bản đối tượng không?

Giá trị của biến đối tượng có thể được sao chép sang một biến khác trong PHP bằng cách sử dụng nhân bản đối tượng.Trong PHP, một bản sao nông của đối tượng có thể được tạo bằng cách sử dụng bản sao từ khóa và một bản sao sâu của đối tượng có thể được tạo bằng cách sử dụng phương thức __clone [].. In PHP, a shallow copy of the object can be created by using the keyword clone, and a deep copy of the object can be created by using the method __clone[].

Làm cách nào để sao chép một đối tượng trong PHP?

Một bản sao đối tượng được tạo bằng cách sử dụng từ khóa Clone [gọi phương thức __clone [] của đối tượng nếu có thể].$ copy_of_Object = Clone $ Đối tượng;Khi một đối tượng được nhân bản, PHP sẽ thực hiện một bản sao nông của tất cả các thuộc tính của đối tượng.Bất kỳ thuộc tính nào là tài liệu tham khảo cho các biến khác sẽ vẫn là tài liệu tham khảo. [which calls the object's __clone[] method if possible]. $copy_of_object = clone $object; When an object is cloned, PHP will perform a shallow copy of all of the object's properties. Any properties that are references to other variables will remain references.

Đối tượng trong PHP là gì?

Một đối tượng là một thể hiện riêng lẻ của cấu trúc dữ liệu được xác định bởi một lớp.Chúng tôi xác định một lớp một lần và sau đó tạo ra nhiều đối tượng thuộc về nó.Đối tượng còn được gọi là trường hợp.an individual instance of the data structure defined by a class. We define a class once and then make many objects that belong to it. Objects are also known as instances.

Chủ Đề