PHP Object Clone Clone usage example _php tips

Source: Internet
Author: User

The example in this article describes the cloning clone usage of PHP objects. Share to everyone for your reference, specific as follows:

Shallow clones: Is simply a clone of a non-object, non-resource data, in which the property in the object is stored as an object type, the cloning is incomplete

<?php
class b{public
 $val = ten;
}
Class a{public
 $val =;
 public $b;
 Public Function __construct () {
  $this->b = new B ();
 }
}
$obj _a = new A ();
$obj _b = Clone $obj _a;
$obj _a->val =;
$obj _a->b->val =;
Var_dump ($obj _a);
Echo ' <br> ';
Var_dump ($obj _b);

The results of the operation are as follows:

Object (A) [1] public '
 val ' => int public
 ' B ' => 
 object (b) [2] public
  ' val ' => int

Object (A) [3] public '
 val ' => int to
 ' B ' => 
 object (b) [2] public
  ' Val ' => int 40

Deep Cloning: all attribute data for an object is copied thoroughly, and the Magic method is used to __clone () and to implement deep cloning inside

<?php
class b{public
 $val = ten;
}
Class a{public
 $val =;
 public $b;
 Public Function __construct () {
  $this->b = new B ();
 }
 Public Function __clone () {
  $this->b = Clone $this->b;
 }
}
$obj _a = new A ();
$obj _b = Clone $obj _a;
$obj _a->val =;
$obj _a->b->val =;
Var_dump ($obj _a);
Echo ' <br> ';
Var_dump ($obj _b);

The results of the operation are as follows:

Object (A) [1] public '
 val ' => int public
 ' B ' => 
 object (b) [2] public
  ' val ' => int

Object (A) [3] public '
 val ' => int to
 ' B ' => 
 object (b) [4] public
  ' val ' => int 10

More about PHP Interested readers can view the site topics: "PHP object-oriented Program Design Primer", "PHP basic Grammar Introductory Course", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", "PHP Array" operation Skills Encyclopedia, " Summary of PHP string usage, Introduction to PHP+MYSQL database operations, and a summary of PHP common database operations Tips

I hope this article will help you with the PHP program design.

Related Article

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.