Home >Backend Development >PHP Tutorial >Copying and cloning objects in php

Copying and cloning objects in php

无忌哥哥
无忌哥哥Original
2018-06-28 15:35:092455browse

* Copying and cloning of objects

* 1. By default, objects are passed by reference (actually a copy of the object identifier, which will be discussed in detail later)

* 2. In other words, the two object variables actually refer to the same object

* 3. If you want to create a new object, you must use the clone keyword to clone the current object

* 4 .When using the clone keyword, if there is __clone() in the class, it will be automatically called

* 5. The __clone() method runs on the newly created object

* 6.__clone( ) method can control what we copy and the basic operations to be completed when cloning

class Member
{
    //声明三个私有属性
    private $name;  //会员名
    private $email; //会员邮箱
    private $score; //会员积分
    
    //构造方法
    public function __construct($name='',$email='',$score=0)
    {
        $this->name = $name;
        $this->email = $email;
        $this->score = $score;
    }
    
    //查询器(暂时省略访问控制)
    public function __get($name)
    {
        return $this->$name;
    }
    
    //设置器(暂时省略访问控制)
    public function __set($name,$value)
    {
        $this->$name = $value;
    }
    
    //克隆魔术方法在对象克隆时自动调用,针对新对象进行初始化操作
    public function __clone()
    {
        $this->score = 0;
    }
    
}

//Instantiate the member class Member and create the member object $member

$member = new Member('peter','peter@php.cn',1000);

//Access test

echo $member->score;

//Copy the member object

$member1 = $member;

//Use the new variable name $member1 to update the object information

$member1->score = 2000;

//Use the new object variable name $member1 to access

echo $member1->score;
echo &#39;<hr>&#39;;

//Use the original object name $member to access

echo $member->score;

* Conclusion:

* 1. The result of the new variable modification is reflected in the original object variable

* 2 .Explain that these two variables actually point to the same object

* 3. That is, the object is passed by reference by default

* 4. In other words, the second variable name $member1 It is just an alias of the original variable, and no new object is created

* 5. It can be understood that it is just a new name for the identifier of the original object

var_dump($member1);  //对象id=1
var_dump($member);   //对象id=1

//Explanation$ member1 and $member are two identical objects, just with different names

//What should you do if you want to completely create a brand new object? You need to use the keyword: clone

$member2 = clone $member;

//Let’s first check the score attribute value of the object variable $member2

echo $member2->score; //目前是原始值2000

//Modify the score value

$member2->score = 5000;
echo &#39;<hr>&#39;;

//Check the score in $member2 again

echo $member2->score; //新值5000
echo &#39;<hr>&#39;;

//Think about it, will my modification affect the score attribute value of the original $member variable?

echo $member->score; //发现原对象的score属性值仍为2000,未发生变化

//Why is this? Because we used cloning technology to create Two completely different object variables

//Verification below

var_dump($member); //变量id是1
var_dump($member2); //变量id是2

//The IDs of these two object variables are different, indicating that they are two completely different objects

//The following is a task to complete: When creating a new member object, clear the member's points to zero? What should be done?

//Create a __clone() clone in the Member class Magic method, preprocess the cloned new object

//Demonstrate it again

//View the current points

echo $member->score;

//Clone to create a brand new object $member3

$member3 = clone $member;

//Check the score value of the new object $member3 to verify whether __clone() in the class is effective?

echo &#39;<hr>&#39;;
echo $member3->score; //0,说明克隆魔术方法已经生效

The above is the detailed content of Copying and cloning objects in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn