Created
December 25, 2016 11:48
-
-
Save gorkamu/9aa957ea198f2e4e13c729e92bd5b9e6 to your computer and use it in GitHub Desktop.
Ejemplo de método mágico __clone()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class SubObject | |
{ | |
static $instances = 0; | |
public $instance; | |
public function __construct() { | |
$this->instance = ++self::$instances; | |
} | |
public function __clone() { | |
$this->instance = ++self::$instances; | |
} | |
} | |
class MyCloneable | |
{ | |
public $object1; | |
public $object2; | |
function __clone() | |
{ | |
// Forzamos la copia de this->object, si no | |
// hará referencia al mismo objeto. | |
$this->object1 = clone $this->object1; | |
} | |
} | |
$obj = new MyCloneable(); | |
$obj->object1 = new SubObject(); | |
$obj->object2 = new SubObject(); | |
$obj2 = clone $obj; | |
print("Objeto Original:\n"); | |
print_r($obj); | |
print("Objeto Clonado:\n"); | |
print_r($obj2); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment