[Document] clone usage in PHP

Source: Internet
Author: User
ArticleDirectory
    • Clone usage in PHP
Clone usage in PHP

Official documents: http://cn2.php.net/__clone

In most cases, we do not need to completely copy an object to obtain its attributes. However, you need to: If you have a GTK window object, the object holds window-related resources. You may want to copy a new window to keep all attributes the same as the original window, but it must be a new object (because if it is not a new object, changes in one window will affect the other window ). Another case is: If object A contains the reference of object B, when you copy object A, you want to use the object instead of object B but a copy of object B, then you must get a copy of object.

Object replication can be completed by using the clone keyword (if possible, the _ clone () method of the object will be called ). The _ clone () method in the object cannot be called directly.

 

Demo 1
<? PHP  Class  Person {  Private   $ Name  ; Private   $ Age  ;  Private   $ ID  ;  Function _ Construct ( $ Name , $ Age  ){  $ This -> Name = $ Name  ;  $ This -> Age = $ Age ;}  Function Setid ( $ ID  ){  $ This -> Id = $ ID  ;}  Function  _ Clone (){  $ This -> Id = 0 ;}}  Print "<PRE>" ;  $ Person = New Person ("Bob", 44 );  $ Person -& Gt; setid (343 );  $ Person2 = Clone   $ Person  ;  Print_r ( $ Person  );  Print_r ( $ Person2  );  Print "</PRE>" ; ?>
DEMO 2
<? PHP  Class  Account {  Public   $ Balance  ;  Function _ Construct ( $ Balance  ){  $ This -> Balance = $ Balance  ;}}  Class  Person {  Private  $ Name  ;  Private   $ Age  ;  Private   $ ID  ;  Public   $ Account  ;  Function _ Construct ( $ Name , $ Age , Account $ Account  ){ $ This -> Name = $ Name  ;  $ This -> Age = $ Age  ;  $ This -> Account = $ Account  ;}  Function Setid ( $ ID  ){  $ This -> Id = $ ID  ;} Function  _ Clone (){  $ This -> Id = 0 ;}}  $ Person = New Person ("Bob", 44, New Account (200 ));  $ Person -& Gt; setid (343 );  $ Person2 = Clone   $ Person  ; //  Give $ person some money  $ Person -> Account-> balance + = 10 ;  //  $ Person2 sees the credit too  Print   $ Person2 -> Account-> Balance;  //  Output: // 210 ?>

 

 

 

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.