Getting Started with PHP, clone and __clone

Source: Internet
Author: User

Objective

This article mainly introduces PHP programming in the __clone () method using the detailed, __clone () method equivalent to a shallow copy, is the basic knowledge of PHP primer learning, the need for friends can refer to.

1 objects are reference data types, when you assign an object to another object by using =, the address of the object is assigned, two variables point to the same address, and the other one changes

__construct () Function: Creates a new SimpleXMLElement object. If successful, the function returns an object. If it fails, it returns false.

Original $zhangsan:

Class person{public    $name;    public $age;    function __construct ($name, $age) {        $this->name= $name;        $this->age= $age;    }} $zhangsan =new person ("Zhang San"), Var_dump ($zhangsan);

Set up a Lisi

$lisi = $zhangsan;

Set the age of Lisi to 28.

$lisi->age=28;

Printing Zhang San and John Doe at the same time will show:

2clone: If you want to completely clone an object to a non-interfering object, you need to use the Clone keyword;

Put John Doe clone Zhang San and set age to 28.

$lisi =clone $zhangsan; $lisi->age=28;

Print Zhang San and John Doe at this time.

3__clone ();


① the Magic method is automatically invoked when cloning an object using the Clone keyword;
The ②__clone () function, which is equivalent to the constructor for cloning an object, is used to assign an initial value to a new cloned object;
③ in the Clone () function, the $this points to the object that is new to Cologne.

function __clone () {        echo "execute clone command <br>";        $this->name= "John Doe";    }

@ Only Yun Hee

Getting Started with PHP, clone and __clone

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.