Home php教程 php手册 第五节--克隆

第五节--克隆

Jun 13, 2016 pm 12:37 PM

/*
+-------------------------------------------------------------------------------+
| = 本文为Haohappy读> 
| = 中Classes and Objects一章的笔记 
| = 翻译为主+个人心得 
| = 为避免可能发生的不必要的麻烦请勿转载,谢谢 
| = 欢迎批评指正,希望和所有PHP爱好者共同进步! 
| = PHP5研究中心: http://blog.csdn.net/haohappy2004
+-------------------------------------------------------------------------------+
*/

第五节--克隆

PHP5中的对象模型通过引用来调用对象, 但有时你可能想建立一个对象的副本,并希望原来的对象的改变不影响到副本 . 为了这样的目的,PHP定义了一个特殊的方法,称为__clone. 像__construct和__destruct一样,前面有两个下划线.

默认地,用__clone方法将建立一个与原对象拥有相同属性和方法的对象. 如果你想在克隆时改变默认的内容,你要在__clone中覆写(属性或方法).

克隆的方法可以没有参数,但它同时包含this和that指针(that指向被复制的对象). 如果你选择克隆自己,你要小心复制任何你要你的对象包含的信息,从that到this. 如果你用__clone来复制. PHP不会执行任何隐性的复制, 

下面显示了一个用系列序数来自动化对象的例子:

复制代码 代码如下:

   class ObjectTracker //对象跟踪器  
   {  
       private static $nextSerial = 0;  
       private $id;  
       private $name;  

       function __construct($name) //构造函数  
       {  
           $this->name = $name;  
           $this->id = ++self::$nextSerial;  
       }  

       function __clone()  //克隆  
       {  
           $this->name = "Clone of $that->name";  
           $this->id = ++self::$nextSerial;  
       }  

       function getId() //获取id属性的值  
       {  
           return($this->id);  
       }  

       function getName() //获取name属性的值  
       {  
           return($this->name);  
       }  
   }  

   $ot = new ObjectTracker("Zeev's Object");  
   $ot2 = $ot->__clone();  

   //输出: 1 Zeev's Object  
   print($ot->getId() . " " . $ot->getName() . "
");  

   //输出: 2 Clone of Zeev's Object  
   print($ot2->getId() . " " . $ot2->getName() . "
");  
?>  

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)